views:

61

answers:

1

I've encountered a strange occurrence when I pass a long long value back from a method call. In my app I've already worked around this but I am curious to know why this is.

Can someone explain to me, when I have a method as defined:

- (long long)testLong {
    NSString* longString = @"100000133597162";
    long long retval = [longString longLongValue];
    NSLog(@"retval:%lld",retval);
    return retval;
}

and I call the method like so:

long long test = [self testLong];
NSLog(@"test:%lld",test);

The results are:

retval:100000133597162
test:410044394

It seems so bizarre to me. The correct value is printed out while inside the method. But once outside the method it seems to be a totally different value. I have stepped through using the debugger and it reveals the same thing - two different values. I just can't seem to explain it. Anyone?

+3  A: 

It's not a "totally different value"; 410044394 is the low 32 bits of 100000133597162. You almost certainly have simply neglected to include a header file, or have the method declared as returning a long or int somewhere.

If your calling function can't figure out what the correct return type is, it will assume that it is type id, which is 32 bits wide on the iPhone, which would produce exactly the truncation that you're seeing here.

Similarly, if your calling function has a prototype that declares the return type to be something 32 bits wide on the iPhone (like long or int), you will see the same truncation effect.

Stephen Canon
you are correct. I neglected to leave it out the prototype out of the header actually. Once i stuck it in there everything looks good. Thank you
WillF
Glad to hear it.
Stephen Canon