Pergunta

Criei uma lista de propriedades com o nome Propertys.plist. Então eu escrevi isso:

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 executo esse código novamente, sempre recebo o número 0 no primeiro NSLog e o número 1 no segundo. Por quê?

Este código é para o iPhone.

Foi útil?

Solução

[plistData objectAtIndex:1];

Este é um NSNumber. Portanto, o endereço desse objeto Objc é convertido em um número inteiro e atribuído a a, dando o valor estranho. Usar -intValue Para extrair um número inteiro dele.

int a = [[plistData objectAtIndex:1] intValue];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top