无法在文档目录中找到plist文件?如何程序化修改plist ?

时间:2022-03-22 19:04:50

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.

我已经找到了来自application main bundle的数据,并将数据输入到数组中。

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

但是由于我想在运行时修改plist数据,所以在Documents目录中找不到它

Actually I want to modify the plist programatically.Though I am not using mainbundle.

实际上我想程序化地修改plist。虽然我没有使用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?

1 个解决方案

#1


6  

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

如果有任何问题,请告诉我,谢谢

#1


6  

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

如果有任何问题,请告诉我,谢谢