简体   繁体   中英

My plist is changed, how to replace the changed strings in documents directory

My plist is an array with dictionaries .

At launch, the .plist is copied from bundle to documents directory if it doesn't exist there already.

But if the plist already exists in documents directory :

each dictionary must be checked against its updated twin dictionary in the bundle to look for changes in the "District" string.

And of course, replace the string if there has been made a change.

This is the copy plist function:

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
[self copyPlist];
return YES;
}

- (void)copyPlist {

NSError *error;
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"Wine.plist"];
NSString *bundle = [[NSBundle mainBundle] pathForResource:@"Wine" ofType:@"plist"];
NSFileManager *fileManager = [NSFileManager defaultManager];

if (![fileManager fileExistsAtPath: path]) {
    [fileManager copyItemAtPath:bundle toPath:path error:&error];
} else {
//I need to check if the "District" value has been changed in any of the dictionaries.
}
}

Any suggestion for how this could be done, or useful tutorials/sample codes?

My guess is that I have to extract the content of the plists into NSMutableArrays: bundleArray and documentsArray . Then find the matching dictionaries in the arrays. Could be done by looking at "Name" string to be equal. Then compare the two "District" strings in the matching dictionaries and look for any changes, and replace the changed ones. But I have no idea how it should be done so any help is very useful as this is very important!

I think your Dictionary Structure is as follows: Root as "Array"

  1. Dictionary 1 with "District as one of the key"

  2. Dictionary 2 with "District as one of the key"

You can check if two NSDictionary at particular indexes of Array are equal or not, which I have coded below.

NSArray *bundleArray=[[NSArray alloc] initWithContentsOfFile:@"path to .plist in bundle"];;
NSArray *documentArray=[[NSArray alloc] initWithContentsOfFile:@"path to .plist in DocumentDirectory"];
BOOL updateDictionary=NO;

for(int i=0;i<bundleArray.count;i++){
    NSDictionary *bundleDic=[bundleArray objectAtIndex:i];

    NSDictionary *documentDic=[documentArray objectAtIndex:i];

    if(![bundleDic isEqualToDictionary:documentDic])
    {
        /*
         *if there is any change between two dictionaries. 
         * i.e bundle .plist has changed so update .plist in document Directory
         */

        [documentDic setValue:[bundleDic objectForKey:@"District"] forKey:@"District"];
        updateDictionary=YES;

    }
}

//Update Dictionary
if(updateDictionary){
    [documentArray writeToFile:@"path to .plist in DocumentDirectory" atomically:YES];
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM