简体   繁体   English

关于将布尔文件写入plist

[英]About write boolean file to the plist

I have a question on writing Boolean value to the plist. 我对将布尔值写入plist有疑问。 The wired thing is the dir for plist before write and read is totally the same, but I can not write to the boolean value to it. 有线的东西是写和读之前的plist的目录是完全相同的,但是我无法向其写入布尔值。 how can I solve this problem? 我怎么解决这个问题? btw: I got the write result in iPhone simulator but failed in real iPhone debug. 顺便说一句:我在iPhone模拟器中得到了写入结果,但是在真正的iPhone调试中失败了。

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

试过这个吗?

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

Finally, I know the principle of the plist. 最后,我知道plist的原理。

plist in bundle is read only. 捆绑软件中的plist是只读的。 If you want to write data to the plist programmatically, you have to move the plist form the bundle to the document directory, then you can edit the data no matter write or read... 如果要以编程方式将数据写入plist,则必须将plist从捆绑中移至文档目录,然后无论写入还是读取,都可以编辑数据...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM