关于向plist写boolean文件

问题描述:

我有一个向plist写入布尔值的问题。 有线的东西是写入和读取之前plist的目录是完全相同的,但我不能写入它的布尔值。我怎么解决这个问题? btw:我在iPhone模拟器中获得了写入结果,但在真正的iPhone调试中失败。关于向plist写boolean文件

//--- CODE HERE ---// 
[ContentList objectAtIndex: paramSender.tag] setValue: [NSNumber numberWithBool: TRUE] forKey: BooleanKey]; // set object boolean value to be ture; 
[ContentList writeToFile: self.plistPath atomically: YES]; // update the plist 
NSMutableArray *tmp = [[NSMutableArray alloc] initWithContentsOfFile: self.plistPath]; // get the content from the updated plist 
NSLog(@"the bool value is %@", [[ContentList objectAtIndex: paramSender.tag] objectForKey: BooleanKey]); 
NSLog(@"After update boolValue is %@", [[tmp objectAtIndex: paramSender.tag] objectForKey: BooleanKey]); 

//--- OUTPUT IN NSLOG ---// 
2012-04-23 18:09:12.164 iPhoneTips[151:707] the bool value is 1 
2012-04-23 18:09:12.167 iPhoneTips[151:707] After update boolValue is 0 

//--- FINISH ---// 

试试这个:

NSLog(@"After update boolValue is %d", [[[tmp objectAtIndex: paramSender.tag] objectForKey: BooleanKey] boolValue]); 
+0

不,结果仍然相同真正的iphone。 – DavidBear 2012-04-23 09:29:21

+0

我想知道为什么在模拟器和真正的iPhone – DavidBear 2012-04-23 09:29:54

试过这种?

[[ContentList objectAtIndex: paramSender.tag] setBool:YES forKey: BooleanKey]; 
+0

之间的结果是不同的。我不认为这里的问题。由于contentlist可以改变对象的布尔值,但不能写入原始plist ...但是,仍然thx提前~~ – DavidBear 2012-04-23 11:47:10

最后,我知道的plist的原则。

包中的plist是只读的。如果要以编程方式将数据写入plist,则必须将plist表格捆绑到文档目录,然后无论写入还是读取,都可以编辑数据...