Actualizar clave en el diccionario

I have a few NSDictionaries inside of an NSArray. I want to modify a BOOL inside one of the dictionaries so that the whole array will have that change (it loads a tableview).

I have this code but it doesn't change the BOOL. I feel like I need to remove the mutableCopy to work, but then it crashes...

    NSLog(@"before: %@", self.currentScopeArray);
    if (cell.accessoryType == UITableViewCellAccessoryNone) {
        cell.accessoryType = UITableViewCellAccessoryCheckmark;
        [[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:YES] forKey:@"enabledByDefault"];
    } else {    
        if (!(self.currentScopeArray.count < 2)) {
            cell.accessoryType = UITableViewCellAccessoryNone;
            [[[self.currentScopeArray objectAtIndex:iIndexPath.row]mutableCopy] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"];
        }
    }
    NSLog(@"after: %@", self.currentScopeArray);

preguntado el 01 de febrero de 12 a las 22:02

2 Respuestas

You are adding a bunch of NSDictionary to a NSArray. I would suggest that you

  • Make the NSArray into a NSMutableArray
  • Make the NSDictionary into an NSMutableDictionary

Why? because you already need to modify values in the Dictionary. And, chances are that you may Además, want to add/remove dictionaries from the array. Once you have made them mutable, you can then do this:

   [[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:YES] forKey:@"enabledByDefault"];

y la

[[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"];

Another problem with With your current implementation is that you are operating on the copia mutable y la no on the original dictionary. The copy is modified, the original is not.

Respondido 02 Feb 12, 03:02

If you create a mutable copy of the object then it'll be exactly that, a copy. If you want to mutate the object that is already inside currentScopeArray then either you need to have mutable objects in the array already, or you need to get a mutable copy, mutate it and then replace the original object.

I would suggest the former to be honest. So you would create your currentScopeArray añadiendo NSMutableDictionarys to it and then just replace this line:

[[[self.currentScopeArray objectAtIndex:iIndexPath.row]mutableCopy] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"];

con este:

[[self.currentScopeArray objectAtIndex:iIndexPath.row] setObject:[NSNumber numberWithBool:NO] forKey:@"enabledByDefault"];

Respondido 02 Feb 12, 03:02

So you removed mutableCopy from that line, but what else do I need to do? The app crashes if thats all I do. - Jon

You need your dictionaries in that array to be NSMutableDictionarys. How are you creating the array in the first place? - Mattjgalloway

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.