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

3 回答 3

0

试过这个?

[[ContentList objectAtIndex: paramSender.tag] setBool:YES forKey: BooleanKey];
于 2012-04-23T09:52:33.203 回答
0

试试这个:

NSLog(@"After update boolValue is %d", [[[tmp objectAtIndex: paramSender.tag] objectForKey: BooleanKey] boolValue]);
于 2012-04-23T09:23:31.687 回答
0

终于知道plist的原理了。

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

于 2012-04-24T03:41:50.550 回答