2012-09-08 97 views
0

我使用的是结构化与字典数组来填充我的应用程序一个plist中。 plist中被存储在包中。我曾经在一些字典(如拼写纠错)取得了一些字符串一些变化。找到匹配的NSDictionary

appDidFinishLaunchingWithOptions调用copyPlist的plist中复制到文件目录,如果它不存在。因此,如果plist中确实存在,我需要检查每一个字典一些字符串更改,并替换这些字符串。

我做了两个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. 
} 

我如何安排配套NSDictionaries所以他们的一些字符串可以比拟的?该Name字符串是不变的,所以这可能是用于识别匹配。

示例代码或引用有用的教程或示例代码将是巨大的,因为我自己的研究并没有导致任何有用的东西,我真的需要纠正。

回答

1

一个plist中可以直接读入这样的词典:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 

如果你这样做了两个Plist档案,您可以使用匹配的密钥从另一个像这样的更新其中的一个:

- (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]; 
     } 
    } 
} 

在开始之前,你要制作一部得到更新可变的字典中,像这样:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path]; 
NSMutableDictionary *dictA = [dict mutableCopy]; 
+0

一个方位正是我想要的!谢谢。 – ingenspor