Why does an NSInteger variable have to be cast to long when used as a format argument?

后端 未结 5 1120
借酒劲吻你
借酒劲吻你 2020-12-02 06:56
NSInteger myInt = 1804809223;
NSLog(@\"%i\", myInt); <==== 

The code above produces an error:

Values of type \'NSInteger\

5条回答
  •  旧时难觅i
    2020-12-02 07:30

    Instead of passing an NSInteger to NSLog, just pass an NSNumber. This will get around all the casts and choosing the right string format specifier.

    NSNumber foo = @9000;
    NSLog(@"foo: %@", foo);
    NSInteger bar = 9001;
    NSLog(@"bar: %@", @(bar));
    

    It also works for NSUIntegers without having to worry about that. See answer to NSInteger and NSUInteger in a mixed 64bit / 32bit environment

提交回复
热议问题