gpt4 book ai didi

objective-c - 找到匹配的 NSDictionary

转载 作者:行者123 更新时间:2023-11-29 04:26:06 24 4
gpt4 key购买 nike

我正在使用一个结构为带有字典的数组的 plist 来填充我的应用程序。 plist 存储在包中。我对某些词典中的某些字符串进行了一些更改(例如拼写更正)。

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 字符串未更改,因此可用于识别匹配项。

代码示例或引用有用的教程或示例代码会很棒,因为我自己的研究没有带来任何有用的结果,我真的需要纠正这个问题。

最佳答案

plist 可以直接读入字典,如下所示:

NSDictionary *dict = [NSDictionary dictionaryWithContentsOfFile:path];

如果您对两个 plist 执行此操作,则可以使用另一个 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];

关于objective-c - 找到匹配的 NSDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12335168/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com