无法读取并保存/数字转换成一个的.plist
-
21-09-2019 - |
题
我创建具有名称propertys.plist一个属性列表。然后写这样的:
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];
当我再次运行该代码,我总是在第一的NSLog在第二数字0和1号。为什么呢?
这是代码的iPhone。
解决方案
[plistData objectAtIndex:1];
这是一个NSNumber。因此,这ObjC对象的地址被转换为一个整数,并分配给a
,给人奇怪值。使用-intValue
从中提取一个整数。
int a = [[plistData objectAtIndex:1] intValue];
不隶属于 StackOverflow