【问题标题】:How do I convert NSInteger to NSString datatype?如何将 NSInteger 转换为 NSString 数据类型?
【发布时间】:2010-12-20 06:19:57
【问题描述】:

如何将NSInteger 转换为NSString 数据类型?

我尝试了以下方法,其中月份是NSInteger

  NSString *inStr = [NSString stringWithFormat:@"%d", [month intValue]];

【问题讨论】:

    标签: ios iphone nsstring nsinteger


    【解决方案1】:

    NSInteger 不是对象,您将它们转换为 long,以匹配当前 64 位架构的定义:

    NSString *inStr = [NSString stringWithFormat: @"%ld", (long)month];

    【讨论】:

    • 我试过这个,但我一直收到警告Format specifies type 'int' but the argument has type 'NSInteger *'(aka 'int *')。相反,根据Apple docs,我选择了NSString *inStr = [NSString stringWithFormat:@"%d", (int)month];
    • 请注意,在 64 位处理器上,例如新的 A7 芯片,如果您的应用程序是为 64 位编译的,则 NSInteger 实际上是 long,而不是 int。对于通用案例,在 64 位平台上进行强制转换 (int)month 将具有破坏性。如果专门针对 Apple 平台,则更喜欢 Aleksey Kozhevnikov 的回答中的 Objective-C 方式,或者类似的方式,它可以同时用于 int 和 long ——例如long ;-) Andreas Ley 的回答中有一个例子,虽然没有符号(意思是非负数)。
    • @Steven 我试图删除答案,以便让当前、更适用的答案浮出水面并被接受,但显然,不能删除已接受的答案。因此,我尝试至少调整其内容,以便为寻求不会触发警告的快速解决方案的人们提供尽可能多的有用信息。
    • @luvieere Apple's documentation 清楚地解释了如何处理格式字符串中的 NSInteger。您应该更新您的答案以遵循此建议。
    • [@(integerValue) stringValue] 是一种更简洁的方法。
    【解决方案2】:

    给出了答案,但认为在某些情况下,这也是从 NSInteger 获取字符串的有趣方式

    NSInteger value = 12;
    NSString * string = [NSString stringWithFormat:@"%0.0f", (float)value];
    

    【讨论】:

      【解决方案3】:

      Obj-C 方式 =):

      NSString *inStr = [@(month) stringValue];
      

      【讨论】:

        【解决方案4】:

        在支持arm64 的情况下编译时,不会产生警告:

        [NSString stringWithFormat:@"%lu", (unsigned long)myNSUInteger];
        

        【讨论】:

          【解决方案5】:

          现代 Objective-C

          NSInteger 具有 stringValue 方法,即使是文字也可以使用

          NSString *integerAsString1 = [@12 stringValue];
          
          NSInteger number = 13;
          NSString *integerAsString2 = [@(number) stringValue];
          

          非常简单。不是吗?

          斯威夫特

          var integerAsString = String(integer)
          

          【讨论】:

            【解决方案6】:

            你也可以试试:

            NSInteger month = 1;
            NSString *inStr = [NSString stringWithFormat: @"%ld", month];
            

            【讨论】:

              【解决方案7】:

              在这种情况下,NSNumber 可能对你有好处。

              NSString *inStr = [NSString stringWithFormat:@"%d", 
                                  [NSNumber numberWithInteger:[month intValue]]];
              

              【讨论】:

                【解决方案8】:

                %zd 适用于 NSIntegers(%tu 适用于 NSUInteger),在 32 位和 64 位架构上都没有强制转换和警告。我不知道为什么这不是“recommended way”。

                NSString *string = [NSString stringWithFormat:@"%zd", month];
                

                如果您对它的工作原理感兴趣,请参阅this question

                【讨论】:

                  【解决方案9】:

                  简单的方法:

                  NSInteger value = x;
                  NSString *string = [@(value) stringValue];
                  

                  这里@(value) 将给定的NSInteger 转换为NSNumber 对象,您可以为其调用所需的函数stringValue

                  【讨论】:

                    猜你喜欢
                    • 1970-01-01
                    • 2012-03-13
                    • 1970-01-01
                    • 1970-01-01
                    • 2011-12-04
                    • 2015-05-30
                    • 1970-01-01
                    • 1970-01-01
                    • 1970-01-01
                    相关资源
                    最近更新 更多