Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get object for key from NSDictionary?

In dictionary named dict the key value pair is:

"alba\U2019s window" = "A model in westindies.";

And to send objectForKey: I am getting the string like "alba's window". When I send like following:

[dict objectForKey:alba's window];

I am not getting anything, it is showing null.

like image 780
Univer Avatar asked Dec 13 '11 09:12

Univer


2 Answers

For starters, you might want to make that an actual string, like so:

[dict objectForKey:@"alba's window"];

Note also that \U2019 is not '; but , which is a different character entirely.

And more generally, sticking to [a-zA-Z0-9]+ for dictionary keys is probably a good idea, unless you are inserting and retrieving programmatically using the exact same string as a key.

like image 92
Williham Totland Avatar answered Oct 02 '22 22:10

Williham Totland


Since iOS6 onwards, a convenient method for setting and accessing the object for a key from an NSDictionary is:

//Setting the object in NSMutableDictionary
dictionary[@"someKey"] = @"someString";

//Accessing the object
NSString *str = dictionary[@"someKey"];
like image 23
ZeMoon Avatar answered Oct 02 '22 22:10

ZeMoon