Reputation: 932
I'm using a plist structured as an array with dictionaries to populate my app. The plist is stored in the bundle. I have made some changes in some strings in some of the dictionaries (like spelling corrections).
appDidFinishLaunchingWithOptions
calls copyPlist
to copy the plist to documents directory if it doesn't exist. So if the plist does exist, I need to check some strings in every dictionary for changes, and replace these strings.
I made two NSMutableArrays
:
if ([fileManager fileExistsAtPath: documentsDirectoryPath]) {
NSMutableArray *newObjectsArray = [[NSMutableArray alloc] initWithContentsOfFile:documentsDirectoryPath];
NSMutableArray *oldObjectsArray = [[NSMutableArray alloc] initWithContentsOfFile:bundlePath];
//Then arrange the dictionaries that match so some their strings can be compared to each other.
}
How can I arrange the matching NSDictionaries
so some of their strings can be compared? The Name
string is unchanged, so this could be used to recognize a match.
Code examples or reference to useful a tutorial or sample codes would be great, as my own research hasn't lead to anything useful and I really need to correct this.
Upvotes: 0
Views: 89
Reputation: 62676
A plist can be read directly into a dictionary like this:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
If you do this for two plists, you can update one of them using matching keys from the other like this:
- (void)updateDictionary:(NSMutableDictionary *)dictA withMatchingKeysFrom:(NSDictionary *)dictB {
// go through all the keys in dictA, looking for cases where dictB contains the same key
// if it does, dictB will have a non-nil value. use that value to modify dictA
for (NSString *keyA in [dictA allKeys]) {
id valueB = [dictB valueForKey:keyA];
if (valueB) {
[dictA setValue:valueB forKey:keyA];
}
}
}
Before you start, you'll want to make the dictionary that's getting updated mutable, like this:
NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];
NSMutableDictionary *dictA = [dict mutableCopy];
Upvotes: 1