Question

I created a property list with the name propertys.plist. Then I wrote this:

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

When I run this code again, I always get number 0 in the first NSLog and number 1 in the second. Why?

This code is for the iPhone.

Was it helpful?

Solution

[plistData objectAtIndex:1];

This is an NSNumber. So the address of this ObjC object is converted to an integer and assigned to a, giving the strange value. Use -intValue to extract an integer from it.

int a = [[plistData objectAtIndex:1] intValue];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top