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

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

The code above produces an error:

Values of type \'NSInteger\

5条回答
  •  我在风中等你
    2020-12-02 07:27

    You get this warning if you compile on OS X (64-bit), because on that platform NSInteger is defined as long and is a 64-bit integer. The %i format, on the other hand, is for int, which is 32-bit. So the format and the actual parameter do not match in size.

    Since NSInteger is 32-bit or 64-bit, depending on the platform, the compiler recommends to add a cast to long generally.

    Update: Since iOS 7 supports 64-bit now as well, you can get the same warning when compiling for iOS.

提交回复
热议问题