Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

NSDictionary objectForKey is returning nil value

Tags:

objective-c

I have a dictionary like this

123456
{
   "aaa"
   "aaav"
}

But when i try to get these values in NSArray using objectForKey, I am getting nil.

The key I am using is a string.

Code :

NSArray *arr = [userdetails objectForKey:str]

Where userdetails is an NSDictionary and str is NSString which contains the key.

like image 565
shivam shukla Avatar asked Feb 20 '23 15:02

shivam shukla


1 Answers

NSDictionary cannot contain nil objects. There are two possibilities to get a nil back from objectForKey:

  • Your dictionary itself has not been initialized, or
  • There is no object for the key that you have specified.

Please make sure that you have created an instance of your NSDictionary or a compatible class (say, NSMutableDictionary) and assign it to the variable prior to querying it for the key. Then make sure the key in question contains an object.

like image 151
Sergey Kalinichenko Avatar answered Mar 06 '23 01:03

Sergey Kalinichenko