【问题标题】:Which is the best way to create NSString from an int?从 int 创建 NSString 的最佳方法是什么?
【发布时间】:2014-12-10 08:29:14
【问题描述】:

假设我有这个,int yo = 5;

现在我想将其转换为 NSString。以下是可能的方法,

1) NSString *h = [NSString stringWithFormat:@"%d",yo];

2) NSString *k = @(yo).stringValue;

3) NSString *s = [NSNumber numberWithInt:yo].stringValue;

4) any other "best" ways?

哪个是最好的,为什么?

【问题讨论】:

    标签: cocoa-touch nsstring type-conversion processing-efficiency memory-efficient


    【解决方案1】:

    第一种方法更好,因为它避免了创建 NSNumber 的中间实例。另请注意,

    @(5).stringValue;
    

    [@(5) discriptionWithLocale:nil];
    

    是等价的语句。

    【讨论】:

      猜你喜欢
      • 2020-03-03
      • 2017-08-06
      • 2012-12-28
      • 2022-01-05
      • 2017-04-13
      • 1970-01-01
      • 2013-01-12
      • 1970-01-01
      • 2016-04-13
      相关资源
      最近更新 更多