我有这样的问题:我有一个plist文件,我把它添加到来自其他项目的追索文件中,现在我可以阅读它,但我现在不知道如何更改它。如果可以,请帮帮我。
答案 0 :(得分:1)
我们无法直接写入我们的应用程序包,因此我们需要将plist复制到应用程序的文档目录中以供此用途:
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPlistName.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if (![fileManager fileExistsAtPath: path])
{
NSString *bundle = [[NSBundle mainBundle] pathForResource:@”yourPlistName” ofType:@”plist”];
[fileManager copyItemAtPath:bundle toPath: path error:&error];
}
然后编辑plist使用:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"yourPlistName.plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];
if ([fileManager fileExistsAtPath: plistPath ])
{
NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
[infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"yourKey"];
[infoDict writeToFile:plistPath atomically:YES];
}
此处yourPlistName.plist
是您的plist,yourKey
是您为其设置新值的关键。
有关详情,请查看此video。