iphone - 在文档目录中找不到 plist 文件?如何以编程方式修改 plist?

标签 iphone objective-c xcode plist

我在 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/

相关文章:

ios - 在 ios 中从数组中获取完整的对象

iphone - 如何使用我自己的颜色组合更改 UITableViewCell 的背景颜色

ios - iPhone 不会忘记测试用户帐户(应用内购买)

IOS 应用程序未显示在 Firebase Analytics 中

xcode - 将应用程序从 Xcode 部署到设备而不启动应用程序

iphone - 使用全局 NSCache 还是每个类一个?

iphone - 如何子类化 UITextField 并覆盖 drawPlaceholderInRect 以更改占位符颜色

ios - 使用 AFNetworking 从 S3 获取公共(public)文件并接收 403

objective-c - 指向 objective-c 中的指针?

xcode - 宽度和水平位置不明确