无法从 .plist 读取数字或将数字保存到 .plist 中
我创建了一个名为properties.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,在第二个 NSLog 中得到数字 1。为什么?
此代码适用于 iPhone。
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.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
这是一个 NSNumber。因此,这个 ObjC 对象的地址被转换为整数并分配给
a
,给出了奇怪的值。使用-intValue
从中提取整数。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.