How to write the data but do not repeat the plist?
The following code has been correctly write plist, but no way to detect whether there is to write the same data.
How to do it does not already joined the data to repeat the join.
-(void)saveData{
// Create the new dictionary that will be inserted into the plist.
NSMutableDictionary *nameDictionary = [NSMutableDictionary dictionary];
[nameDictionary setValue:woonam开发者_如何学编程e forKey:@"wooname"];
[nameDictionary setValue:woodata forKey:@"woodata"];
[nameDictionary setValue:woojclass forKey:@"woojclass"];
[nameDictionary setValue:woojtime forKey:@"woojtime"];
[nameDictionary setValue:woojpin forKey:@"woojpin"];
[nameDictionary setValue:woojsps forKey:@"woojsps"];
[nameDictionary setValue:woojspall forKey:@"woojspall"];
[nameDictionary setValue:wooUrl forKey:@"wooUrl"];
NSString *rootPath = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *plistPath = [rootPath stringByAppendingPathComponent:@"myData.plist"];
NSMutableArray *plist = [NSMutableArray arrayWithContentsOfFile:plistPath];
if (plist == nil) plist = [NSMutableArray array];
[plist addObject:nameDictionary];
[plist writeToFile:plistPath atomically:YES];
[plist insertObject:nameDictionary atIndex:0];
If I understand your question:
Make the plist a dictionary, not an array then read the existing plist into a NSMutableDictionary
, add the new/updated values, write it out. Or instead of
[plist addObject:nameDictionary
use
[plist addEntriesFromDictionary:nameDictionary];
精彩评论