简体   繁体   English

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

[英]Can not find plist file in documents directory? How to modify the plist programmatically?

I have an application in Xcode 4.2. 我在Xcode 4.2中有一个应用程序。

I have created plist file as my requirement. 我已经创建了plist文件作为我的要求。

I have found the data from Applications main bundle and got the data in to the array. 我找到了Applications main bundle中的数据,并将数据输入到数组中。

But As I want to modify the plist data at run time, I could not find it in Documents Directory 但是,由于我想在运行时修改plist数据,我在Documents Directory中找不到它

Actually I want to modify the plist programmatically. 其实我想以编程方式修改plist。 Though I am not using mainbundle. 虽然我没有使用mainbundle。

-(NSString *) saveFilePath::(NSString *)tagString     
{
    NSArray *arr = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);

    NSString *strPath = [[arr objectAtIndex:0] stringByAppendingPathComponent:tagString];       
    return strPath;
} 

Could not retrive the plist file.(Path I am getting, but path with file not getting) 无法检索plist文件。(我得到的路径,但文件未获取的路径)

I also could not find physically the plist file in the iphone simulator/App/Documents folder? 我也找不到iphone模拟器/ App / Documents文件夹中的plist文件?

you need to copy plist from bundle to document directory first 您需要先将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]);
    }
}

let me know in case of any query thanks& regards neil 如果有任何疑问请告诉我,谢谢和问候尼尔

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM