我正在更新一个不支持64位架构的旧iOS应用程序。
从苹果的转换过程看,它似乎将“整型和无符号整型分别转换为NSInteger和NSUInteger”。然而,当我这样做的时候,我会在格式化的过程中遇到很多到long的类型转换,如下所示。我会担心在32位架构上的性能,因为所有的转换都是long。
//old
NSInteger myIntValue = 5;
[NSString stringWithFormat:@"%d", myIntValue];
//new
NSInteger myIntValue = 5;
[NSString stringWithFormat:@"%ld", (long)myIntValue];另一种方法是用int替换我的NSInteger用法,这样它们仍然是4字节值,这样在格式化myIntValue时就不需要强制转换为long。
//old
NSInteger myIntValue = 5;
[NSString stringWithFormat:@"%d", myIntValue];
//new
int myIntValue = 5;
[NSString stringWithFormat:@"%d", myIntValue];假设从NSInteger更改为int的项将保证适合4字节的int,出于任何特殊原因,是否应避免使用后一种方法?
发布于 2015-10-22 21:31:04
在为32位和64位编译时,这些强制转换是格式化字符串的唯一方法,因为另一种方法更糟糕(在我看来):
#if __LP64__
[NSString stringWithFormat:@"%ld", myIntValue];
#else
[NSString stringWithFormat:@"%d", myIntValue];
#endif对于强制转换,没有什么重要的性能问题需要担心,因为这只是一个堆栈上有多少空间的问题,也许还有一些符号扩展。一些次要的东西。
https://stackoverflow.com/questions/33281906
复制相似问题