Sto riscontrando questo strano problema. NSDictionary non restituisce il valore intero corretto.Come ottenere un valore intero da NSDictionary?
Codice di risposta JSON dal server.
{
"status":"ok",
"error_code":0,
"data" : [],
"msg":"everything is working!"
}
Il JSON viene convertito in un NSDictionary.
NSError *error = nil;
NSDictionary *jsonDict = [NSJSONSerialization
JSONObjectWithData:data
options:NSJSONReadingMutableContainers
error: &error];
Accedo al valore NSDictionary utilizzando il seguente codice.
int error_code = (int)[jsonDict valueForKey:@"error_code"]
NSLog(@"%i", error_code);
The log outputs the following: 143005344
Ho anche provato objectForKey e ottengo la stessa risposta.
Grazie, in anticipo.
Si dovrebbe usare 'int' e' intValue', oppure 'NSInteger' e' integerValue', ma non si devono mescolarli senza un cast esplicito. – bdesham
Grazie, ha funzionato. – James
Ebbene sì, il puntatore dell'oggetto, che è NSnumber – Lefteris