有没有简单的方法来替换plist中的一个数据?

时间:2010-05-11 11:12:15

标签: objective-c iphone plist

以下是我如何获得价值:

NSString *path =  [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:path];
NSArray *tempArray = [dict valueForKey:@"2"];
NSString *myTarget = [tempArray objectAtIndex:0];

“myTarget”是我要替换的东西,如何用@“newString”替换它并写入plist?你好。

1 个答案:

答案 0 :(得分:3)

这样做:

NSString *path =  [[NSBundle mainBundle] pathForResource:@"myPlist" ofType:@"plist"];
NSData * data = [NSData dataWithContentsOfFile:path];
NSMutableDictionary * dict = (NSMutableDictionary *)[NSPropertyListSerialization
                                    propertyListFromData:data
                                    mutabilityOption:NSPropertyListMutableContainersAndLeaves
                                    format:NULL
                                    errorDescription:NULL];
NSMutableArray *tempArray = [dict valueForKey:@"2"];
NSString *myTarget = [tempArray objectAtIndex:0];
[tempArray replaceObjectAtIndex:0 withObject:newString];
[dict writeToFile:[path stringByExpandingTildeInPath] atomically:YES];