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

余生颓废 提交于 2019-12-09 01:08:39

问题


I have an application in Xcode 4.2.

I have created plist file as my requirement.

I have found the data from Applications main bundle and got the data in to the array.

But As I want to modify the plist data at run time, I could not find it in Documents Directory

Actually I want to modify the plist programmatically. Though I am not using 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)

I also could not find physically the plist file in the iphone simulator/App/Documents folder?


回答1:


you need to copy plist from bundle to document directory first

-(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



来源:https://stackoverflow.com/questions/7754668/can-not-find-plist-file-in-documents-directory-how-to-modify-the-plist-programm

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!