2013-01-14 11 views
7

Ho un NSArray che mi dà i seguenti dati:Creazione di un NSMutableArray da un NSArray Objective-C

01/14/2013 13:28:06.559 IUser Reader [71164: c07] (
     { 
     "id_acompanhante" = ""; 
     "id_evento" = 34; 
     "user_id" = 1; 
     "inserido_por" = "Himself"; 
     name = iUser; 
     status = done; 
     type = User; 
    } 
     { 
     "id_acompanhante" = 1; 
     "id_evento" = 34; 
     "user_id" = 1; 
     "inserido_por" = iUser; 
     name = "Mayara Roca"; 
     status = naofeito; 
     type = companion; 
    } 
) 

Come faccio a giocare a questo i dati ad un NSMutableArray e aggiungi un altro campo all'interno di ogni elemento. esempio:

{ 
    "id_acompanhante" = ""; 
    "id_evento" = 34; 
    "user_id" = 1; 
    "inserido_por" = "Himself"; 
    name = IUser; 
    status = done; 
    type = User; 
    tag = 2; 
} 

Ho aggiunto un altro campo "TAG".

Come posso fare?

+0

è tutto all'interno del '{}' il contenuto del tuo array o è esso che rappresenta una matrice individuale. – Souljacker

+0

Ho inserito il codice nella mia risposta per aggiungere un campo TAG a ciascun dizionario all'interno dell'array. – Fogmeister

+0

se si aggiunge un valore alla chiave "TAG" nei dizionari, si dovrebbe andare con la risposta di Fogmeister. – Souljacker

risposta

8
NSMutableArray *mutableArray = [NSMutableArray array]; 

for (NSDictionary *dictionary in yourArray) { 
    NSMutableDictionary *mutDictionary = [dictionary mutableCopy]; 

    [mutDictionary setObject:[NSNumber numberWithInt:2] forKey:@"tag"]; 

    [mutableArray addObject:mutDictionary]; 
} 

Questo è come fare quello che hai chiesto. Rendere la matrice mutabile non ti permetterà di cambiare il contenuto di un dizionario al suo interno. È necessario estrarre ciascun dizionario e renderlo mutabile e quindi memorizzarlo nell'array.

+0

Eccellente !!! Grazie –

6

Facile

NSMutableArray *mArray = [NSMutableArray arrayWithArray:yourArray] 
[mArray addObject:yourObject]; 
+0

Questo non è quello che viene chiesto. – Fogmeister

0

Prova questo

NSMutableArray *aMutableArray = [NSMutableArray arrayWithArray:anArray]; 
1

E 'facile come chiamare mutableCopy sul vostro array:

NSArray * myArray = @[@"one", @"two", @"three"]; 
NSMutableArray * myMut = [myArray mutableCopy]; 
NSLog(@"My Mut = %@", myMut); 

Speranza che aiuta!

Aggiornamento Ecco un esempio che scorre oltre la matrice originale, converte elementi a dizionario mutevole e inserisce la nuova proprietà. Quindi getti la matrice originale e usi la versione myMut.

NSArray * myArray = @[@{@"key1":@"value1"}, @{@"key2":@"value2"}]; 
NSMutableArray * myMut = [[NSMutableArray alloc] init]; 
for (NSDictionary * dict in myArray) { 
    NSMutableDictionary * mutDict = [dict mutableCopy]; 
    [mutDict setObject:@"2" forKey:@"tag"]; // Add new properties 
    [myMut addObject:mutDict]; 
} 
+0

Il mio male, non ho letto completamente la tua domanda. La mia risposta ti permetterà di convertire l'array in mutevole, ma sembra che tu abbia una serie di NSDictionaries. Per aggiungere una proprietà al dizionario; dovrai rendere il dizionario mutabile. – Dave

0

Per quanto ho capito, non è necessario un array mutevole ma è necessario convertire gli NSDictionaries all'interno dell'array per essere mutabili. Questo è anche risposto da altri.

Ma voglio mostrare un trucco elegante come è possibile impostare aggiungere una coppia chiave/valore a tutti i dizionari mutabili con la linea. Io uso dati molto più semplici come te, in modo che sia più facile vedere, cosa succede.

//just an example array of mutable Dictionary 
NSMutableArray *array = [NSMutableArray array]; 
[array addObject:[NSMutableDictionary dictionaryWithObject:@"one" forKey:@(1)]]; 
[array addObject:[NSMutableDictionary dictionaryWithObject:@"two" forKey:@(2)]]; 
[array addObject:[NSMutableDictionary dictionaryWithObject:@"three" forKey:@(3)]]; 

// and here is the trick 
[array setValue:@"10" forKey:@"tag"]; 

La matrice contiene ora

(
{ 
    tag = 10; 
    1 = one; 
}, 
{ 
    tag = 10; 
    2 = two; 
}, 
{ 
    3 = three; 
    tag = 10; 
} 
) 

from the NSArray docs

setValue:forKey:
Richiama setValue: Forkey: su ciascuna delle voci della matrice con il valore e la chiave specificata.

- (void)setValue:(id)value forKey:(NSString *)key 
0
NSArray *theOldArray; // your old array 
NSMutableArray *theMutableAry = [NSMutableArray array]; 
for (NSDictionary *dicItem in theOldArray) 
{ 
    NSMutableDictionary *dicWithOneMoreField = [NSMutableDictionary dictionaryWithDictionary:dicItem]; 
    [dicWithOneMoreField setValue:@"your value for tag" forKey:@"tag"]; 
    [theMutableAry addObject:dicWithOneMoreField]; 
} 

o provare il contrario

NSData* archivedData = [NSKeyedArchiver archivedDataWithRootObject:theOldArray]; 
NSMutableArray *mutableArray = [NSKeyedUnarchiver unarchiveObjectWithData:archivedData]; 
for (NSDictionary *dicItem in mutableArray) 
{ 
    [dicItem setValue:@"your value for tag" forKey:@"tag"]; 
}