gpt4 book ai didi

ios - NSMutableDictionary 比较提取不等于对

转载 作者:行者123 更新时间:2023-11-29 03:22:41 27 4
gpt4 key购买 nike

我有两个 NSMutableDictionay,我们将预期的一个称为“A”,将操作结果的一个称为“B”

我想将“A”和“B”之间的差异插入“C”NSMutableDictionary。

我使用 if (![dicExpected isEqualToDictionary:actualDic]) {

但是获得非常见对的最佳方法是什么?

最佳答案

该问题包含一些含糊之处。我认为其意图可能是假设这两个词典具有匹配的键并输出“b”词典值作为差异。下面的代码执行此操作,但考虑了其他解释:

// use the keys in dA to find non-matching values in dB

- (NSDictionary *)nonMatching:(NSDictionary *)dA comparedTo:(NSDictionary *)db {

NSMutableDictionary *diff = [NSMutableDictionary dictionary];

for (id key in [dA allKeys]) {
id valueA = dA[key];
id valueB = dB[key];
if (valueB && ![valueA isEqual:valueB]) {

// here, the question is ambiguous. what should go in the output
// for non-matching values? the b value? both?
// both would look like this: diff[key] = @[valueA, valueB];
// but lets guess that you want just the b value

diff[key] = valueB;
}
// another ambiguity in the question is what to do with keys in the dA that
// don't appear in dB. One idea is to ignore them, which is what happens here
// without more code. that's probably what you need. anther idea would
// be to record a null in the output for those, like this:
// if (!valueB) diff[key] = [NSNull null];
}

// the last ambiguity is dealing with dB keys that don't appear in dA
// again, do nothing is probably the idea you're looking for, but this would work, too:

// for (id keyB in [dB allKeys]) {
// if (!dA[keyB]) diff[keyB] = dB[keyB];
// }

return diff;
}

关于ios - NSMutableDictionary 比较提取不等于对,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20886634/

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