【问题标题】:How do I pull an integer out of a NSDictionary and put it in an integer?如何从 NSDictionary 中提取一个整数并将其放入一个整数中?
【发布时间】:2009-09-22 22:42:34
【问题描述】:

我有一个使用 plist 初始化的 NSDictionary,其中包含 count 后跟 32,我想获取 count 的值。

怎么做?

我知道如何通过

将其放入对象中
[dictionaryObjectName objectForKey:@"count"]

但对我来说,我得到的价值并不是一个对象。

如果我必须指定一个对象,最好使用什么(仅供参考,该值将始终是无符号的),我需要将其转换为真正的 int。

我会做类似的事情

NSNumber *num = [dictionaryObjectName objectForKey:@"count"];
int theValue = [num intValue];
[num release];

对于没有垃圾收集器的 iPhone 来说,在 num 上发布是一件好事吗?

【问题讨论】:

  • int theValue = [num intValue]; // 是一个 'int' AND NSInteger theValue = [num integerValue]; // 是一个 NSInteger

标签: iphone nsdictionary


【解决方案1】:

更好的形式是:

int theValue = [[dictionaryObjectName objectForKey:@"count"] intValue];

编辑

由于我看到人们仍然访问此页面,让我们澄清一下,这些天你只是这样做

int theValue = [dictionaryObjectName[@"count"] intValue];

【讨论】:

    【解决方案2】:

    是的,您将其作为NSNumber 拉出,然后抓住int(eger)Value,但无需释放它。您没有保留、分配或复制该号码,因此您不必担心释放它。

    【讨论】:

      猜你喜欢
      • 2023-03-20
      • 1970-01-01
      • 2013-10-08
      • 2020-02-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2020-10-25
      相关资源
      最近更新 更多