开发者

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

开发者 https://www.devze.com 2022-12-29 17:46 出处:网络
Here is how I can get the value: NSString *path =[[NSBundle mainBundle] pathForResource:@\"myPlist\" ofType:@\"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

精彩评论

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