1

For some reason I can't get an array added to a nsmutabledictionary. I have it declared as a property in my .h file here:

@interface TCMExhibitFeedStore : NSObject

@property (nonatomic, strong) NSMutableDictionary *allLevels;
@property (nonatomic, strong) NSMutableDictionary *storedLevels;

+ (TCMExhibitFeedStore *)sharedStore;

- (void)fetchExhibtFeedWithCompletion:(void (^)(NSMutableDictionary *obj, NSError *err))block;
- (TCMLevel *)createLevel:(TCMLevelRemote *)l;
- (TCMExhibit *)createExhibit:(TCMExhibitRemote *)e;

- (BOOL)saveChanges;

@end

Then, I'm trying to add an empty array to it in a function in the .m file like this

[_storedLevels setObject:[[NSMutableArray alloc] init] forKey:@"levels"];

However, when I step through the application this never gets added. Looking in the debugger shows it as

_storedLevels   NSMutableDictionary *   0x00000000

NSMutableDictionaries are nothing new to me. I'm just going a bit crazy trying to find my error because this all looks normal to me.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
LoneWolfPR
  • 3,978
  • 12
  • 48
  • 84
  • The above line attempts to set a mutable array as the value of the key "levels" in the dictionary _storedLevels. It fails, however, since _storedLevels is nil, having never been initialized. (Note that it's foolish to attempt to "daisy chain" statements together when debugging, especially if you don't really understand what you're doing. Do one operation per statement, and put each statement on a separate line.) – Hot Licks Jul 22 '13 at 21:26

2 Answers2

4

The following lines confuses me...

[_storedLevels setObject:[[NSMutableArray alloc] init] forKey:@"levels"];

Instead of above Use:

 self.storedLevels = [[NSMutableDictionary alloc] init];
 [self.storedLevels setObject:... forKey:@"levels"];

NOTE: Whenever you see 0x00000000

This means your object is not alloc-ated.

Therefore you need to alloc+init them, before setting any array/value to them.

Anoop Vaidya
  • 46,283
  • 15
  • 111
  • 140
2

0x00000000 means nil. All properties and instance variables of Objective C objects are initialized to nil. Hence, we can tell that you have never initialized your properties.

You can to initialize the dictionaries in the designated initializer for TCMExhibitFeedStore. (Or before you access them to add elements)

self.storedLevels = [NSMutableDictionary dictionary];
self.allLevels = [NSMutableDictionary dictionary];
Sonny Saluja
  • 7,193
  • 2
  • 25
  • 39