开发者

Unexpected results when returning long long in a method call

I've encountered a strange occurrence when I pass a long l开发者_运维问答ong 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?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜