0

我的 plist 是一个array with dictionaries.

在启动时,如果 .plist不存在,则从bundleto复制它。documents directory

但是如果 plist 已经存在于documents directory

必须根据包中更新的双字典检查每个字典,以查找“District”字符串中的更改。

当然,如果进行了更改,请替换字符串。

这是复制 plist 函数:

- (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.
}
}

关于如何做到这一点的任何建议,或有用的教程/示例代码?

我的猜测是我必须将 plist 的内容提取到 NSMutableArrays:bundleArraydocumentsArray. 然后在数组中找到匹配的字典。可以通过查看“名称”字符串是否相等来完成。然后比较匹配字典中的两个“District”字符串并查找任何更改,并替换更改的。但我不知道应该怎么做,所以任何帮助都非常有用,因为这非常重要!

4

1 回答 1

1

我认为您的字典结构如下: Root as "Array"

  1. 字典 1 与“地区作为关键之一”

  2. 字典 2 与“地区作为关键之一”

您可以检查 Array 的特定索引处的两个NSDictionary是否相等,我在下面进行了编码。

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];
}
于 2012-09-09T14:51:54.240 回答