ios - NSUInteger 字符串编码 32 位和 64 位

标签 ios cocoa-touch 64-bit

我正在尝试找出将 NSUInteger 转换为适用于 32 位和 64 位系统的字符串的最佳方法。我看到两个平台的 NSUInteger 定义不同,我想确保我所做的是正确的。

我见过的所有解决方案都需要一些转换,或者看起来不正确。我收到以下代码的警告,要求我添加显式强制转换。

NSUInteger unsignedInteger = PostStatusFailed; // This is an enum
NSString *string = [NSString stringWithFormat:@"postStatus == %lu", unsignedInteger];

我也尝试过 %d(有符号整数,这似乎不正确)和 %lx,但似乎都不正确。谢谢。

最佳答案

来自String Programming Guide :

OS X uses several data types—NSInteger, NSUInteger,CGFloat, and CFIndex—to provide a consistent means of representing values in 32- and 64-bit environments. In a 32-bit environment, NSInteger and NSUInteger are defined as int and unsigned int, respectively. In 64-bit environments, NSInteger and NSUInteger are defined as long and unsigned 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/

相关文章:

ios - arm64 标志类似于 arc 标志 (-fno-objc-arc)

ios - 如何从 Swift 中的 Firebase 树中删除 UItableView 中的引用 autoId 值?

ios - 与业务合作伙伴分享 iOs 应用程序演示

ios - 将 UIPickerView 放置在 UIAlertController 的中心

php - 64 位 php 时间戳问题

cocoa - 保持传统 Carbon 代码的活力

ios - 在 Firebase block 外使用变量

ios - 对齐 UIToolBar 项目

ios - 我如何扩展具有 appDidEnterBackground 依赖项的 NSOperationQueue?

ios - UISteppper 与 textField 连接