如何从NSDictionary中提取一个整数并将其置于整数中?

时间:2021-09-08 11:17:15

I've got an NSDictionary that was initialized with a plist that, among other things, contained count followed by 32 and I want to get at the value for count.

我有一个用plist初始化的NSDictionary,除其他外,包含计数后跟32,我想得到count的值。

How?

怎么样?

I know how to get it into an object via

我知道如何通过它进入一个对象

[dictionaryObjectName objectForKey:@"count"]

But to me, the value I'm obtaining is not an object.

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

If I have to specify an object, what would be the best one to use (FYI the value will always be unsigned) and I need to get it into a true int.

如果我必须指定一个对象,那么最好使用的是什么(FYI值总是无符号的),我需要将它变成一个真正的int。

Do I do something like

我能做点什么吗

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

Is the release on num a good thing to do since this for an iPhone with no garbage collector?

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

2 个解决方案

#1


24  

Yes, you pull it out as an NSNumber, then grab the int(eger)Value but there's no need to release it. You didn't retain, alloc, or copy the number, so you don't have to worry about releasing it.

是的,你把它作为NSNumber拉出来,然后获取int(eger)值,但是没有必要释放它。您没有保留,分配或复制该号码,因此您不必担心将其释放。

#2


27  

And nicer form is:

更好的形式是:

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

EDIT

编辑

Since I see that people still arrive to this page, let's clarify that these days you simply do

既然我看到人们仍然到达这个页面,那么让我们澄清一下,这些天你只是这样做了

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

#1


24  

Yes, you pull it out as an NSNumber, then grab the int(eger)Value but there's no need to release it. You didn't retain, alloc, or copy the number, so you don't have to worry about releasing it.

是的,你把它作为NSNumber拉出来,然后获取int(eger)值,但是没有必要释放它。您没有保留,分配或复制该号码,因此您不必担心将其释放。

#2


27  

And nicer form is:

更好的形式是:

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

EDIT

编辑

Since I see that people still arrive to this page, let's clarify that these days you simply do

既然我看到人们仍然到达这个页面,那么让我们澄清一下,这些天你只是这样做了

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