我在xcode项目的资源中有一个带有Array类型的根的plist文件.在按钮上单击我需要访问此plist并查找plist是否已包含特定项目,如果没有将新项目(NSString)写入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");
}
}
Run Code Online (Sandbox Code Playgroud)
这对我在模拟器上有效,但是当我在设备上运行时,writeToFile:atomically方法似乎总是返回NO.
我甚至尝试过使用NSArchiver,即使这似乎也没有将NO作为返回值
谁能说出我做错了什么..
问候,
Syed Yusuf
您无法写入应用程序的包,因此无法在Resources目录中写入plist.一个好方法是在第一次启动时将plist从Resource目录复制到Documents目录,并在将来从那里访问它.
| 归档时间: |
|
| 查看次数: |
7575 次 |
| 最近记录: |