【问题标题】:Can not find plist file in documents directory? How to modify the plist programmatically?在文档目录中找不到 plist 文件?如何以编程方式修改 plist?
【发布时间】:2011-10-13 13:13:45
【问题描述】:

我在 Xcode 4.2 中有一个应用程序。

我已根据要求创建了 plist 文件。

我从 Applications main bundle 中找到了数据并将数据放入数组中。

但由于我想在运行时修改 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文件复制到documents目录了吗?
  • 什么? Xcode 5.0?只有 Xcode 4.2
  • 不,我认为不需要 Xcode 会自动创建 plist 文件。
  • @Arpit 你能告诉我们你创建plist的代码吗?
  • 你基本上是在做偏好吗?如果是这样,最好使用 NSDefaults 来完成,它将处理合理的默认值,将设置放在正确的位置等。

标签: iphone objective-c xcode plist


【解决方案1】:

你需要先将plist从bundle复制到文档目录

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

如有任何疑问,请告诉我 感谢和问候 尼尔

【讨论】:

    猜你喜欢
    • 2011-01-18
    • 1970-01-01
    • 2011-01-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-04-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多