我在 Xcode 4.2 中有一个应用程序。
我已经根据需要创建了 plist 文件。
我已经从应用程序主包中找到数据并将数据放入数组中。
但是由于我想在运行时修改plist数据,所以在Documents Directory中找不到它
实际上我想以编程方式修改 plist。虽然我没有使用 mainbundle。
-(NSString *) saveFilePath::(NSString *)tagString
{
NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *strPath = [[arr objectAtIndex:0] stringByAppendingPathComponent:tagString];
return strPath;
}
无法检索 plist 文件。(我得到的路径,但没有得到文件的路径)
我也无法在 iphone 模拟器/App/Documents 文件夹中找到 plist 文件?
最佳答案
你需要先把plist从bundle复制到document目录
-(void) checkAndCreateSetting
{
BOOL success;
NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *writableDBPath= [documentsDirectory stringByAppendingPathComponent:@"Settings.plist"];
success = [fileManager fileExistsAtPath:writableDBPath];
if (success) return;
// The writable database does not exist, so copy the default to the appropriate location.
NSString *defaultDBPath = [[[NSBundle mainBundle] resourcePath] stringByAppendingPathComponent:@"Settings.plist"];
success = [fileManager copyItemAtPath:defaultDBPath toPath:writableDBPath error:&error];
if (!success) {
NSAssert1(0, @"Failed to create writable database file with message '%@'.", [error localizedDescription]);
}
}
如有任何疑问,请告诉我 感谢和问候 尼尔
关于iphone - 在文档目录中找不到 plist 文件?如何以编程方式修改 plist?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7754668/