Question

J'ai créé une liste de propriétés avec le nom propertys.plist. Ensuite, je l'ai écrit ceci:

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];

Quand je lance ce code à nouveau, je reçois toujours le numéro 0 dans le premier NSLog et le numéro 1 dans le second. Pourquoi?

Ce code est pour l'iPhone.

Était-ce utile?

La solution

[plistData objectAtIndex:1];

Ceci est un NSNumber. Ainsi, l'adresse de cet objet ObjC est converti en un entier et affecté à a, donnant la valeur étrange. Utilisez -intValue pour extraire un nombre entier de celui-ci.

int a = [[plistData objectAtIndex:1] intValue];
Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top