NSInteger and NSUInteger in a mixed 64bit / 32bit environment

前端 未结 3 1963
礼貌的吻别
礼貌的吻别 2020-12-01 01:49

I have a fair amount of string format specifiers in NSLog / NSAssert etc. calls which use %d and %u with NSInteger (= int on 32bit) an

3条回答
  •  有刺的猬
    2020-12-01 02:13

    I think the safest way is to box them into NSNumber instances.

    NSLog(@"Number is %@", @(number)); // use the highest level of abstraction
    

    This boxing doesn't usually have to create a new object thanks to tagged pointer magic.

    If you really don't want to use NSNumber, you can cast primitive types manually, as others suggested:

    NSLog(@"Number is %ld", (long)number); // works the same on 32-bit and 64-bit
    

提交回复
热议问题