SmartTree
SmartTree

Reputation: 1471

NSMutableArray is nil. Why?

After I add objects to my NSMutableArray it becomes nil (I check it in NSLog)


Code

guests = [[NSMutableArray alloc]init];
guests = [localDictionary objectForKey:@"guestsArray"];

[guests addObject:@"1"];
[guests addObject:@"12"];
[guests addObject:@"31"];

if(guests == nil)
{
    NSLog(@"WHY?");
}

Do you have any ideas why its happening ? What should I check ?

Upvotes: 0

Views: 474

Answers (2)

Johnmph
Johnmph

Reputation: 3391

You create a NSMutableArray in your first line and then you reassign the variable with an object taken from a dictionary.

If you want add objects from the object in localDictionary with guestsArray key, use this instead :

guests = [[NSMutableArray alloc] initWithArray:[localDictionary objectForKey:@"guestsArray"]];

With that, even if your "guestsArray object" doesn't exist in localDictionary, your guests array will not be nil.

Upvotes: 3

akashivskyy
akashivskyy

Reputation: 45170

It's because [localDictionary objectForKey:@"guestsArray"] is nil.

Upvotes: 8

Related Questions