sto usando NSMutableDictionary
e ha colpito questo errore:"mutazione metodo inviati oggetto immutabile", nonostante oggetto essendo NSMutableDictionary
'NSInternalInconsistencyException', reason: '-[__NSCFDictionary removeObjectForKey:]: mutating method sent to immutable object'
Ecco il codice:
// Turn the JSON strings/data into objects
NSError *error;
NSMutableDictionary *invoiceDictFromReq = [[NSMutableDictionary alloc] init];
// invoiceDictFromReq = (NSMutableDictionary *)[NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error];
invoiceDictFromReq = [NSMutableDictionary dictionaryWithDictionary:[NSJSONSerialization JSONObjectWithData:[request responseData] options:kNilOptions error:&error]];
NSLog(@"invoiceDictFromReq count: %i, key: %@, value: %@", [invoiceDictFromReq count], [invoiceDictFromReq allKeys], [invoiceDictFromReq allValues]);
// Get values and keys from JSON response
self.invoiceDict = [invoiceDictFromReq objectForKey:@"invoice"];
NSNumber *invoiceAmount = [self.invoiceDict objectForKey:@"amount"];
NSNumber *invoiceId = [self.invoiceDict objectForKey:@"id"];
NSNumber *invoiceNumber = [self.invoiceDict objectForKey:@"number"];
NSNumber *checkoutStarted = [self.invoiceDict objectForKey:@"checkoutStarted"];
NSNumber *checkoutCompleted = [self.invoiceDict objectForKey:@"checkoutCompleted"];
NSLog(@"amount: %@, id: %@, number: %@, started: %@, completed: %@", invoiceAmount, invoiceId, invoiceNumber, checkoutStarted, checkoutCompleted);
Tutti i log della console indicano che il i dati vanno bene. Questo è dove le cose cominciano a crollare. passo la proprietà invoiceDict
all'altro controller della vista:
// Pass the invoice to checkoutViewController
[checkoutViewController setInvoiceDict:self.invoiceDict];
In CheckoutViewController.m:
// Change invoice checkoutCompleted to true
// [self.invoiceDict removeObjectForKey:@"checkoutCompleted"];
[self.invoiceDict setObject:[NSNumber numberWithBool:YES] forKey:@"checkoutCompleted"];
L'errore è a [self.invoiceDict setObject...]
. Mi sono assicurato che tutti i dizionari che uso siano NSMutableDictionary
. Ho lasciato alcune delle righe commentate nel codice per mostrare le cose che ho provato e ho colpito un muro di mattoni. Suppongo di poter sempre creare un nuovo dizionario. È il modo migliore per farlo?
Grazie per il suggerimento, ma ancora mi dà lo stesso errore Ora ho invoiceDictFromReq come una proprietà in .h e syntehsized in .m e lo impostai ai dati dalla richiesta json come self.invoiceDictFromReq = [NSMutableDictionary dictionary ...] – okysabeni
Scusa se ho cambiato la mia risposta, non era quello. Vedi se hai ancora il problema. Non è necessario mantenere invoiceDictFromReq perché è una variabile locale, ma eliminare la riga che si crea invoiceDictFromReq per la prima volta perché c'è una perdita lì –
:-). Quando ho visto, "Ma il tuo problema è che stai creando un NSMutableDictionary ma stai impostando self.invoiceDict un dizionario all'interno del tuo mutableDictionary, che non è necessariamente anche un mutevoleDictionary" Sapevo che l'avresti colpito immediatamente. Così sciocco da parte mia. Grazie per segnalarlo. Non avrei dovuto presumere che il parser json nativo restituisse NSMutableDictionary. Grazie mille! – okysabeni