【问题标题】:About write boolean file to the plist关于将布尔文件写入 plist
【发布时间】:2012-04-23 09:17:37
【问题描述】:

我有一个关于将布尔值写入 plist 的问题。 有线的东西是 plist 在写入和读取之前的目录是完全一样的,但我不能写入它的布尔值。我怎么解决这个问题? 顺便说一句:我在 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 ---//

【问题讨论】:

    标签: iphone plist


    【解决方案1】:

    试试这个:

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

    【讨论】:

    • 不,在真正的 iphone 中仍然是相同的结果。
    • 我想知道为什么模拟器和真实 iPhone 的结果不同
    【解决方案2】:

    试过了吗?

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

    【讨论】:

    • 好吧。我不认为这里的问题。由于contentlist可以改变对象的boolean值,但是不能写入原来的plist...不过,还是提前谢谢~~
    【解决方案3】:

    终于知道plist的原理了。

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-04-03
      • 2011-02-05
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多