如何将数据写入现有的plistfile?

时间:2010-12-12 15:42:39

标签: objective-c ios iphone

如何编写新数据现有的plist文件?

我的应用中的示例代码

-(NSString *)mypath { 

    NSArray *path =
    NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
    NSString *myfilename = [path objectAtIndex:0];
    return [myfilename stringByAppendingPathComponent:@"mydata.plist"];

}
[myarray writeToFile:[self mypath] atomically:NO];

但它会覆盖现有文件

我有可能编辑现有文件吗?

1 个答案:

答案 0 :(得分:2)

如果要“编辑”现有文件,则必须从plist文件初始化数组,然后编辑数组。然后,您可以再次将阵列保存到同一位置。

示例:

NSMutableArray *myarray = [[NSMutableArray alloc] initWithContentsOfFile:[self mypath]];
//Perform changes to myarray. e.g. [myarray removeObjectAtIndex:...];
[myarray writeToFile:[self mypath] atomically:NO];