Trying to add a NSMutableDictionary to variable with a block parameter but after addObject always returns null

StackOverflow https://stackoverflow.com/questions/23476056

Question

I'm trying to add a NSMutableDictionary to _ons variable with a block parameter but after addObject always returns null.

@property (atomic, strong) NSMutableArray * ons;

--------------------------------------------------------------------

NSMutableDictionary *on = [[NSMutableDictionary alloc] init];
[on setObject:eventName forKey:@"eventName"];
[on setObject:[callback copy] forKey:@"callback"]; // callback it's a block

[_ons addObject:on];
NSLog(@"_ons: %@", _ons); // always prints null!!!
Was it helpful?

Solution

You are never instantiating _ons. Somewhere in your code, you need to call:

self.ons = [[NSMutableArray alloc] init];

(In case you’re new to Objective-C, your code wasn’t crashing because messages to nil are no-ops that return 0, instead of crashing like in some other languages.)

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top