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

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

문제

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!!!
도움이 되었습니까?

해결책

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.)

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top