suj*_*406 1 iphone objective-c plist ios
我必须修改与bundle一起存储的plist文件中的BOOL值.我能够访问我必须修改的字典.从nslogging我可以看到dict用新值更新,但问题是当我检查捆绑中的plist没有被修改.有关为什么它不更新plist的任何线索
NSString* plistPath = nil;
NSFileManager* manager = [NSFileManager defaultManager];
if (plistPath = [[[NSBundle mainBundle] bundlePath] stringByAppendingPathComponent:@"TopicsList.plist"])
{
if ([manager isWritableFileAtPath:plistPath])
{
NSMutableArray* dictarrays = [NSMutableArray arrayWithContentsOfFile:plistPath];
NSMutableDictionary *dict=[dictarrays objectAtIndex:indexPath.row];
NSLog(@"%@ ",dict );
[dict setObject:[NSNumber numberWithBool:YES] forKey:@"isPaid"];
NSLog(@"%@ ",dict );
[dict writeToFile:plistPath atomically:NO];
NSLog(@"%@ ",dict );
[manager setAttributes:[NSDictionary dictionaryWithObject:[NSDate date] forKey:NSFileModificationDate] ofItemAtPath:[[NSBundle mainBundle] bundlePath] error:&error];
}
}
Run Code Online (Sandbox Code Playgroud)
plist是你资源的一部分吗?不确定我们是否可以在那里编辑一个plist.将plist复制到应用程序的Documents文件夹并在那里进行更新.
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *plistPath = [[paths objectAtIndex:0] stringByAppendingPathComponent:@"TopicsList.plist"];
success = [fileManager fileExistsAtPath:plistPath];
if(!success){
//file does not exist. So look into mainBundle
NSString *defaultPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"TopicsList.plist"];
success = [fileManager copyItemAtPath:defaultPath toPath:plistPath error:&error];
}
Run Code Online (Sandbox Code Playgroud)
现在,您需要对plist进行任何更改或从plist中读取数据,请从Documents文件夹中的副本中读取它.
| 归档时间: |
|
| 查看次数: |
2579 次 |
| 最近记录: |