objective-c - 找到匹配的 NSDictionary

标签 objective-c ios xcode

我正在使用一个结构为带有字典的数组的 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/

相关文章:

php - json 从 mysql (使用 php) 到标签

objective-c - 使用 'launchedTaskWithLaunchPath' Cocoa/objective-c API 启动应用程序

objective-c - 如何让 UIToolbar 在静态表格 View 中正确显示?

iphone - 想要创建一个变量,增加它并获得新值

xcode - 在 xcode 3.2.5 上安装 iOS SDK 4.1

cocoa - 如何导入JSON框架?

ios - Storyboard中的 View 太多 - Xcode 运行缓慢

iphone - 添加导航 Controller 的 UIButton 顶栏,推送到另一个 viewController

objective-c - if 语句在其条件变量设置之前执行

ios - 将 UITableView 添加为 SubView 时发送到实例的无法识别的选择器