修改一个plist不起作用

我必须在我的plist文件修改一个布尔值存储与bundle.i能够访问字典,我必须修改。从nslogging我可以看到,字典更新了新的价值,但问题是当我检查在捆绑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]; } } 

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]; } 

现在,无论您需要对plist进行更改还是从plist读取数据,请从“文档”文件夹中的副本中进行阅读。

您必须将plist存储在文档目录中。 之后,您还必须在离开应用程序后保存plist。 否则,plist是在主包中,不能修改。

有没有错误

检查

 NSError *error = nil [dict writeToFile:plistPath atomically:YES encoding:NSASCIIStringEncoding error:&error]; if (error) { NSLog(@"Error: %@", [error description]); } else { NSLog(@"Success"); }