Jure
Jure

Reputation: 799

<__NSCFDictionary: 0x1557f400> was mutated while being enumerated.'

I have ios 7 application that is running on iphone 4. I have a weird problem, where application crashes inside for loop, because of the error in the title. I checked on SO and it says that error occurs when you change object over which you are iterating. So I copied both variables that I use to temp variables but problem still occurs. Problem happen when first iteration is finished.

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableDictionary * badges = [defaults objectForKey:@"badges"];
    NSMutableDictionary *newBadges = badges;
    for(NSString* key in badges)
    {
        NSDictionary* badge = [badges objectForKey:key];
        if([[badge objectForKey:@"achived"]  isEqual: @"NO"])
        {
            if([self checkBadgeCondition:badge])
            {
                NSMutableDictionary *tempBadge = [badge mutableCopy];
                [self showAlertBadge:badge];
                [tempBadge setObject:@"YES" forKey:@"achived"];
                [newBadges setObject:tempBadge forKey:[tempBadge objectForKey:@"name"]];
            }
        }
    }

Upvotes: 1

Views: 501

Answers (1)

Wain
Wain

Reputation: 119031

newBadges = badges

This isn't a copy, it's just another reference to the same thing. You also should expect a dictionary (or array) coming out of user defaults to be mutable. So, make a mutable copy of it here

newBadges = [badges mutableCopy]

Upvotes: 2

Related Questions