开发者

Is there any easy way to replace one data in plist?

Here is how I can get the value:

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

the "myTarget" is something I want to replace with, how can I replace it with @"n开发者_JAVA技巧ewString" and write to plist? thz u.


Do this:

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];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜