Frank
Frank

Reputation: 107

NSDictionary load information of 18 key-value pairs always read as nil instead

    - (instancetype)initWithDestinationIndex:(NSUInteger)levelsIndex {
          NSString* filepath = [[NSBundle mainBundle]
                          pathForResource:@"Levels" ofType:@"plist"];
          NSDictionary *levels = [NSDictionary dictionaryWithContentsOfFile:filepath];
          NSArray *levelsArray = levels[@"LevelsData"];
          _data = levelsArray[levelsIndex];
          [_verticalB setToInitialStateVertical];
          return self;
    }

I have a plist that is supposed to load information of 18 key-value pairs. The _data of type NSDictionary instance variable (when I run the program and put a breakpoint at that line _data = levelsArray[levelsIndex];) is almost always null, except on one occasion where it is actually had the 18 key-value pairs loaded. Any thoughts as to why it pretty much always is null?

I pass in 0 for my levelsIndex, and the 'LevelsData' is the NSArray that holds the 18 key-value pair dictionaries.

Upvotes: 0

Views: 93

Answers (2)

Iyyappan Ravi
Iyyappan Ravi

Reputation: 3245

if your _data is NSMutableDictionary use the below code,

NSMutableDictionary *_data = [[NSMutableDictionary alloc] init];
[_data setObject:[levelsArray objectAtIndex: levelsIndex] forKey:[NSString stringWithFormat:@"%d", levelsIndex]];

or

for(int i=0; i<[levelsArray count]; i++){
    [_data setObject:[levelsArray objectAtIndex:i] forKey:[NSString stringWithFormat:@"%d",i]];
}

hope its helpful

Upvotes: 1

Hemang
Hemang

Reputation: 27072

It may possible that, you are running app in a "release" scheme. Have you double check? As long as you're getting the result.

To check,

Click on Project Name next to [Run] button. > Edit Scheme > Run > Build Configuration > Debug/Release

Upvotes: 0

Related Questions