Domanda

Ho creato un elenco delle proprietà con il nome propertys.plist. Poi ho scritto questo:

NSString *path = [[NSBundle mainBundle] bundlePath];
NSString *finalPath = [path stringByAppendingPathComponent:@"speicherung.plist"];
NSMutableArray *plistData = [[NSMutableArray arrayWithContentsOfFile:finalPath] retain];
int a = [[plistData objectAtIndex:1] intValue];
NSLog(@"%i", a); // returns always 0 - Even If I set a other number in the plist
a = a + 1;
NSNumber *ff = [NSNumber numberWithInt:a];
[plistData insertObject:ff atIndex:1];
NSLog(@"%@", [plistData objectAtIndex:1]); // returns 1 - right, because 0 + 1 = 1
[plistData writeToFile:finalPath atomically:YES];

Quando ho eseguito di nuovo questo codice, ottengo sempre il numero 0 nella prima NSLog e il numero 1 nel secondo. Perché?

Questo codice è per l'iPhone.

È stato utile?

Soluzione

[plistData objectAtIndex:1];

Questo è un NSNumber. Così l'indirizzo di questo oggetto objC viene convertito in un numero intero e assegnato a a, dando il valore strano. Utilizzare -intValue per estrarre un numero intero da esso.

int a = [[plistData objectAtIndex:1] intValue];
Autorizzato sotto: CC-BY-SA insieme a attribuzione
Non affiliato a StackOverflow
scroll top