1

I have a question on writing Boolean value to the 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. how can I solve this problem? btw: I got the write result in iPhone simulator but failed in real iPhone debug.

//--- 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 ---//

3 Answers 3

0

Try this instead :

NSLog(@"After update boolValue is %d", [[[tmp objectAtIndex: paramSender.tag] objectForKey: BooleanKey] boolValue]);
2
  • No, still the same result in the real iphone.
    – DavidBear
    Apr 23, 2012 at 9:29
  • I'm wondering why the result is different between in simulator and real iPhone
    – DavidBear
    Apr 23, 2012 at 9:29
0

Tried this?

[[ContentList objectAtIndex: paramSender.tag] setBool:YES forKey: BooleanKey];
1
  • well. I do not think the problem here. Since the contentlist can change the boolean value of the object, but can not write to the original plist... but, still thx in advance~~
    – DavidBear
    Apr 23, 2012 at 11:47
0

Finally, I know the principle of the plist.

plist in bundle is read only. 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...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.