views:

210

answers:

1

Hello, 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.

+2  A: 
[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];
KennyTM
Okay now it works, but the code didn´t save the new value. Why?
Flocked
@Flocked: Is that code for iPhone or Mac?
KennyTM
The code is for the iPhone - So iPhone OS
Flocked
interesting: I always get the number 1 with your code, even if I set an other number in the plist.
Flocked
that is really strange - I know what happens, but not why - I set 5 numbers in the property list - Then I wrote objectAtIndex:3 to get the object at Index 2. When I restart the methode I get the object at Index 1 - and so on - So I have to set the objectAtIndex +1 to my wanted indexnumber - The problem is that he always went down - so from 3 to 2 to index 1 and so on...
Flocked
I think my plist was broken - Now it works with a new plist - I searched 6 hours for the error!
Flocked
@Flocked: In iPhoneOS the mainBundle is read only. Write the plist elsewhere.
KennyTM