0

After @implementation and before viewDidLoad I declare my NSMutableDictionary as follows :

NSMutableDictionary *dictionary;

Then in my viewDidLoad method I try this :

[dictionary setObject:@"test" forKey:@"haha"];
NSLog(@"%@", [dictionary objectForKey:@"haha"]);

The NSLog returns a null value :

(null)

I would like to know why that happens, it should return "test", doesnt it ?

Mikev
  • 2,012
  • 1
  • 15
  • 27
  • have you inited your `dictionary` anywhere? like e.g. `dictionary = [NSMutableDictionary new];`...? – holex Mar 06 '19 at 09:35
  • This will work. NSMutableDictionary *dictionary = [NSMutableDictionary dictionary]; [dictionary setObject:@"test" forKey:@"haha"]; NSLog(@"%@", [dictionary objectForKey:@"haha"]); – Apparao Mulpuri Mar 06 '19 at 09:36

1 Answers1

0

You will have to instantiate your dictionary first:

[NSMutableDictionary dictionary] 

Then it will be ready to use.

MCMatan
  • 8,623
  • 6
  • 46
  • 85
  • 1
    Thanks a lot for your answer, this works fine. I used to work with swift and had to go back to some old Obj-C so I forgot those habits. – julienstack Mar 06 '19 at 10:06