简体   繁体   中英

iOS: update object of NSMutableArray in for loop

I need to update / add a key -> value pair in a NSMutableArray.

I would like to add a key with a fix value if the key isn't set already.

I tried the following - but the app is crashing at addObject with a "mutating method sent to immutable object" error:

    NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
    NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:@"bookmarks"];

    for (id bookmark in bookmarks) {

        if ([bookmark objectForKey:@"type"] == NULL){
            [bookmark addObject:@"old" forKey:@"type"];
        }
    }

    [[NSUserDefaults standardUserDefaults] synchronize];

Your outer array contains immutable dictionaries. One option would be the following:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:@"bookmarks"];

[booksmarks enumerateObjectsWithOptions:NSEnumerationConcurrent usingBlock:^(NSDictionary *bookmark, NSUInteger index, BOOL *stop) {
    if (!bookmark[@"type"]) {
        NSMutableDictionary *mutable = [bookmark mutableCopy];
        mutable[@"type"] = @"old";
        [bookmarks replaceObjectAtIndex:index withObject:[mutable copy]];
    }
}];

// Update NSUserDefaults
[[NSUserDefaults standardUserDefaults] setObject:bookmarks forKey:@"bookmarks"];

This should be nice an efficient since it can update multiple dictionaries concurrently.

Try this:

NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];

//SAVING

NSMutableArray *bookmarksSaving = [NSMutableArray new];

[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:@[@"red"] forKeys:@[@"color"]]];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:@[@"yellow"] forKeys:@[@"color"]]];
[bookmarksSaving addObject:[[NSMutableDictionary alloc] initWithObjects:@[@"green",@"new"] forKeys:@[@"color",@"type"]]];

[defaults setObject:bookmarksSaving forKey:@"bookmarks"];
[[NSUserDefaults standardUserDefaults] synchronize];

//MODIFY

NSMutableArray *bookmarks = [defaults mutableArrayValueForKey:@"bookmarks"];

NSUInteger index = 0;
for (NSDictionary *bookmark in bookmarks) {

    NSMutableDictionary *bookmarkMutable = [bookmark mutableCopy];

    if ([bookmarkMutable objectForKey:@"type"] == nil)
    {
        [bookmarkMutable setValue:@"old" forKey:@"type"];
        [bookmarks replaceObjectAtIndex:index withObject:bookmarkMutable];
    }

    index++;
}

[[NSUserDefaults standardUserDefaults] synchronize];

You have to save your bookmarks as NSMutableDictionary to be able to add/remove keys/value

  NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
  NSArray *bookmarks = [defaults objectForKey:@"bookmarks"];
  NSMutableArray *newBookMarks = [bookmarks mutableCopy];
  for (NSDictionary *bookmark in bookmarks) {
    NSMutableDictionary *newBookmark = [bookmark mutableCopy];
    if (![bookmark objectForKey:@"type"]){
      [newBookmark setObject:@"old" forKey:@"type"];
    }
    [newBookMarks addObject:newBookmark];
  }
  [defaults setObject:newBookMarks forKey:@"bookmarks"];
  [defaults synchronize];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM