我正在尝试找出将 NSUInteger 转换为适用于 32 位和 64 位系统的字符串的最佳方法。我看到两个平台的 NSUInteger 定义不同,我想确保我所做的是正确的。
我见过的所有解决方案都需要一些转换,或者看起来不正确。我收到以下代码的警告,要求我添加显式强制转换。
NSUInteger unsignedInteger = PostStatusFailed; // This is an enum
NSString *string = [NSString stringWithFormat:@"postStatus == %lu", unsignedInteger];
我也尝试过 %d
(有符号整数,这似乎不正确)和 %lx
,但似乎都不正确。谢谢。
最佳答案
OS X uses several data types—
NSInteger
,NSUInteger
,CGFloat
, andCFIndex
—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment,NSInteger
andNSUInteger
are defined asint
andunsigned int
, respectively. In 64-bit environments,NSInteger
andNSUInteger
are defined aslong
andunsigned long
, respectively. To avoid the need to use different printf-style type specifiers depending on the platform, you can use the specifiers shown in Table 3. Note that in some cases you may have to cast the value.
查看所有类型的链接。对于NSUInteger
,正确的做法是:
NSUInteger i = 42;
NSString *numberString = [NSString stringWithFormat:@"%lu is the answer to life, the universe, and everything.", (unsigned long)i];
关于ios - NSUInteger 字符串编码 32 位和 64 位,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19391368/