Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I pull an integer out of a NSDictionary and put it in an integer?

Tags:

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.

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.

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?

like image 278
Dale Avatar asked Sep 22 '09 22:09

Dale


2 Answers

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]; 
like image 155
ishahak Avatar answered Nov 04 '22 22:11

ishahak


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.

like image 32
Dave DeLong Avatar answered Nov 04 '22 23:11

Dave DeLong