随着iOS版的64位版本,我们不能使用%d和%u再格式化NSInteger和NSUInteger.因为对于64位,这些是typedef'd long而unsigned long不是int和unsigned int.
因此,如果您尝试使用%d格式化NSInteger,Xcode将发出警告.Xcode对我们很好,并提供了这两种情况的替代品,它包括一个带有l前缀的格式说明符和一个长的类型转换.然后我们的代码基本上是这样的:
NSLog(@"%ld", (long)i);
NSLog(@"%lu", (unsigned long)u);
Run Code Online (Sandbox Code Playgroud)
如果你问我,这是一个痛苦的眼睛.
几天前,Twitter上有人提到格式说明符%zd来格式化签名变量和%tu在32位和64位平台上格式化无符号变量.
NSLog(@"%zd", i);
NSLog(@"%tu", u);
Run Code Online (Sandbox Code Playgroud)
这似乎有效.而且我更喜欢类型转换.
但老实说,我不知道为什么这些工作.现在两者对我来说基本上都是神奇的价值观.
我做了一些研究,并发现z前缀意味着以下格式说明符具有相同的大小size_t.但我完全不知道前缀是什么t意味着.所以我有两个问题:
究竟是什么%zd和%tu意味着什么呢?
是否可以安全使用%zd,%tu而不是苹果建议长期使用?
我知道类似的问题和Apples 64位过渡指南,它们都推荐这种%lu (unsigned long)方法.我要求替代铸造.
我正在尝试将NSInteger转换为NSString以在UIAlerView中显示NSInteger值.但问题是我得到这个错误:"当我试图转换NSInteger时,错误的接收器类型'NSInteger'(又名'long')".
NSInteger:
NSInteger clasesDadas = clases.count;
Run Code Online (Sandbox Code Playgroud)
clases.count是TableView中的行数.
代码是:
-(void)infoAction
{
NSInteger clasesDadas = clases.count;
NSString *inStr = [NSString stringWithFormat:@"%d", [clasesDadas intValue]]; /*HERE IS THE ERROR*/
UIAlertView *alertatiempo = [[UIAlertView alloc] initWithTitle:@"Información" message:inStr delegate:self cancelButtonTitle:@"Aceptar" otherButtonTitles:nil];
[alertatiempo show];
}
Run Code Online (Sandbox Code Playgroud)
谁能帮我?非常感谢你.