Iphone 如何更改plist文件

Iphone 如何更改plist文件,iphone,xcode,Iphone,Xcode,我有这样的问题:我有一个plist文件,我把它添加到项目中的资源文件中,现在我可以读取它,但我不知道如何更改它。如果可以,请帮帮我 我们无法直接写入应用程序包,因此我们需要将plist复制到应用程序的文档目录以供使用: NSError *error; NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentsDirecto

我有这样的问题:我有一个plist文件,我把它添加到项目中的资源文件中,现在我可以读取它,但我不知道如何更改它。如果可以,请帮帮我

我们无法直接写入应用程序包,因此我们需要将plist复制到应用程序的文档目录以供使用:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPlistName.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@”yourPlistName” ofType:@”plist”];

     [fileManager copyItemAtPath:bundle toPath: path error:&error];
}
然后,要编辑plist,请使用:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"yourPlistName.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath: plistPath ]) 
{
    NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    [infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"yourKey"];
    [infoDict writeToFile:plistPath atomically:YES];
}
这里的
yourPlistName.plist
是您的plist,
yourKey
是您正在为其设置新值的键


有关更多信息,请检查此项。

我们无法直接写入应用程序包,因此我们需要将plist复制到应用程序的文档目录中以供使用:

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"yourPlistName.plist"];

NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path])
{
     NSString *bundle = [[NSBundle mainBundle] pathForResource:@”yourPlistName” ofType:@”plist”];

     [fileManager copyItemAtPath:bundle toPath: path error:&error];
}
然后,要编辑plist,请使用:

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *plistPath = [documentsDirectory stringByAppendingPathComponent:@"yourPlistName.plist"];
    NSFileManager *fileManager = [NSFileManager defaultManager];

if ([fileManager fileExistsAtPath: plistPath ]) 
{
    NSMutableDictionary* infoDict = [NSMutableDictionary dictionaryWithContentsOfFile:plistPath];
    [infoDict setObject:[NSNumber numberWithBool:hidden] forKey:@"yourKey"];
    [infoDict writeToFile:plistPath atomically:YES];
}
这里的
yourPlistName.plist
是您的plist,
yourKey
是您正在为其设置新值的键


有关更多信息,请检查此项。

是否要使用代码或其他方式将数据添加到plist?我不想添加,只需更改现有信息并将其保存回plist文件。是否要使用代码更改数据?是,这正是我需要的。有关详细信息,请提供一些代码。是否要使用代码或其他方式将数据添加到plist?我不想添加,只需更改现有信息并将其保存回plist文件。是否要按代码更改?是,这正是我所需要的。请提供一些代码以获取更多信息。这不会起作用,因为您正试图写入应用程序包中的原始plist。应用程序无法写入应用程序包,它们只有对其他几个目录的写入权限。这不起作用,因为您正试图写入应用程序包中的原始plist。应用程序无法写入应用程序包,它们只有对其他几个目录的写入权限。