NSMutableArray writeToFile:atomically总是在设备上返回NO但在模拟器上工作正常 [英] NSMutableArray writeToFile:atomically always returns NO on device but works fine on simulator

查看:226
本文介绍了NSMutableArray writeToFile:atomically总是在设备上返回NO但在模拟器上工作正常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在xcode项目的资源中有一个带有Array类型的根的plist文件。在按钮上单击我需要访问此plist并查找plist是否已包含特定项目,如果没有将新项目(NSString)写入plist。

I have a plist file with root of type Array in the resources in the xcode project. On a button click i need to access this plist and find if the plist already contains the particular item if not write the new item(NSString) to the plist.

我是这样做如下,

NSMutableArray *array = nil;
NSDictionary *dictionary = nil;
  path = [[NSBundle mainBundle] pathForResource:@"MyPlistName" ofType:@"plist"];
array = [[NSMutableArray alloc] initWithContentsOfFile:path];
dictionary = [self.dictionary objectForKey:@"tag"];

if(![array containsObject:dictionary])
{
    [array addObject:dictionary];
    if(![array writeToFile:path atomically:YES])
    {
        NSLog(@".plist writing was unsucessfull");
    }
     }

这对我在模拟器上有效,但是当我运行时它在设备上的writeToFile:atomically方法似乎总是返回NO。

This works for me on the simulator but when i run it on the device the writeToFile:atomically method seems to return NO always.

我甚至尝试过使用NSArchiver,即使这似乎也没有返回NO作为返回值

i even tried with NSArchiver even that seems to return me NO as the return value

任何人都能说出我做错了什么..

Can any one tell what am i doing wrong..

问候,

Syed Yusuf

Syed Yusuf

推荐答案

您无法写入应用程序的包中,因此您无法在资源中写入plist目录。一个好方法是在第一次启动时将plist从Resource目录复制到Documents目录,并在将来从那里访问它。

You can not write into the application's bundle so you can not write to a plist in the Resources directory. One good way is to copy the plist from the Resource directory into the Documents directory on first launch and access it from there in the future..

这篇关于NSMutableArray writeToFile:atomically总是在设备上返回NO但在模拟器上工作正常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆