如何覆盖plist文件的内容?(iPhone/iPad)
我的主应用程序包中有一个plist文件,我想通过我的应用程序更新它。这是我正在使用的代码,问题是plist似乎没有得到更新。我的代码是否不正确,或者是否存在其他问题如何覆盖plist文件的内容?(iPhone/iPad),iphone,objective-c,ios,plist,Iphone,Objective C,Ios,Plist,我的主应用程序包中有一个plist文件,我想通过我的应用程序更新它。这是我正在使用的代码,问题是plist似乎没有得到更新。我的代码是否不正确,或者是否存在其他问题 // Data NSMutableDictionary *data = [[NSMutableDictionary alloc] init]; [data setObject:self.someValue forKey:@"Root"]; // Save the logs NSString *filepath = [[NSBund
// Data
NSMutableDictionary *data = [[NSMutableDictionary alloc] init];
[data setObject:self.someValue forKey:@"Root"];
// Save the logs
NSString *filepath = [[NSBundle mainBundle] pathForResource:@"MyFile" ofType:@"plist"];
[data writeToFile:filepath atomically:YES];
有人能帮我吗?IOS限制对捆绑文件的写入。如果您想要一个可写的plist,您需要将其复制到应用程序的Documents文件夹并写入其中。IOS限制写入捆绑文件。如果你想要一个可写的plist,你需要将它复制到应用程序的Documents文件夹中,然后在那里写入它。以下是我在我的一个应用程序中的操作方法
//get file paths
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"document.plist"];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"bundle.plist"];
//if file exists in the documents directory, get it
if([fileManager fileExistsAtPath:documentPlistPath]){
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
return documentDict;
}
//if file does not exist, create it from existing plist
else {
NSError *error;
BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];
if (success) {
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
return documentDict;
}
return nil;
}
希望这能有所帮助以下是我在一个应用程序中的做法
//get file paths
NSFileManager *fileManager = [NSFileManager defaultManager];
NSArray *documentPaths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [documentPaths objectAtIndex:0];
NSString *documentPlistPath = [documentsDirectory stringByAppendingPathComponent:@"document.plist"];
NSString *bundlePath = [[NSBundle mainBundle] bundlePath];
NSString *bundlePlistPath = [bundlePath stringByAppendingPathComponent:@"bundle.plist"];
//if file exists in the documents directory, get it
if([fileManager fileExistsAtPath:documentPlistPath]){
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
return documentDict;
}
//if file does not exist, create it from existing plist
else {
NSError *error;
BOOL success = [fileManager copyItemAtPath:bundlePlistPath toPath:documentPlistPath error:&error];
if (success) {
NSMutableDictionary *documentDict = [NSMutableDictionary dictionaryWithContentsOfFile:documentPlistPath];
return documentDict;
}
return nil;
}
希望这有帮助您无法更新应用程序包中的任何内容,您需要先将其复制到文档目录中。我不知道这一点。请您就如何实施您的建议提供建议?您无法更新应用程序包中的任何内容,您需要先将其复制到您的文档目录中。我不知道这一点。请您就如何实施您的建议提供建议?谢谢您的帮助,请您就如何实施建议提供建议?看一看,它应该会有所帮助。谢谢@JoachimIsaksson!我被困在这件事上了。似乎iOS允许您覆盖与应用程序捆绑的文件,但在应用程序重新启动时,所有更改都消失了。我被难住了。谢谢你的帮助,你能给我一些建议吗?看看,应该会有帮助的。谢谢@JoachimIsaksson!我被困在这件事上了。似乎iOS允许您覆盖与应用程序捆绑的文件,但在应用程序重新启动时,所有更改都消失了。我被难住了。