简体   繁体   English

“NSString stringWithFormat:”在32位设备中具有“%zd”的奇怪行为

[英]“NSString stringWithFormat:” with “%zd” curious behavior in 32bit devices

Why we cannot format string using "%zd" as expected in 32bit devices(iPhone5, iPhone4s or their simulators)? 为什么我们不能在32位设备(iPhone5,iPhone4s或他们的模拟器)中使用“%zd”格式化字符串?

int64_t test1 = 11111111;
int64_t test2 = 22222222;
int64_t test3 = 33333333;
int64_t test4 = 44444444;
NSString *testStr = [NSString stringWithFormat:@"\"%zd %zd %zd %zd\"", test1, test2, test3, test4];
NSLog(@"testStr: %@", testStr);

NSInteger test5 = 55555555;
NSInteger test6 = 66666666;
NSInteger test7 = 77777777;
NSInteger test8 = 88888888;
testStr = [NSString stringWithFormat:@"\"%zd %zd %zd %zd\"", test5, test6, test7, test8];
NSLog(@"testStr: %@", testStr);

The log is: 日志是:

2017-09-12 05:53:59.462: testStr:"11111111 0 22222222 0"
2017-09-12 05:53:59.465: testStr:"55555555 66666666 77777777 88888888"

Note: Answer just typed in on a tablet, your sample not tested, so this is a guess. 注意:只需在平板电脑上输入答案,您的样品未经过测试,因此这是猜测。

The format %zd is for values of type size_t and not 64-bit values. 格式%zd用于size_t类型的值,而不是64位值。

Try using %lld for int64_t values, long long is 64 bits on both 32-bit and 64-bit iOS versions. 尝试将%lld用于int64_t值,在32位和64位iOS版本上long long为64位。

For NSInteger try %ld and cast the arguments to long , this is because the size of NSInteger is dependent on platform. 对于NSInteger尝试%ld并将参数NSIntegerlong ,这是因为NSInteger的大小依赖于平台。

HTH HTH

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM