修改一个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"); }
- 在didFinishLaunchingWithOptions结束之前,applicationWillResignActive会被调用吗?
- 如何从SKTexture获取原始图像比例?
- UITableView deleteRowsAtIndexPath在删除最后一条logging时崩溃
- Swfit 2“额外的参数”错误“在调用”
- 如何完全着色UIPopoverPresentationController背景色?
- 将由逗号分隔的NSString转换为NSArray
- 使用iOS场景套件加载未压缩的collada文件
- 运行时错误:__NSAutoreleaseNoPool():…自动释放没有到位的池 – 只是泄漏
- 在Swift4中,更换navigationItem.leftBarButtonItem将调整不需要的大小