本文介绍了比较包含NSStrings的NSDictionaries的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个 NSDictionaries 包含 NSStrings 。为了比较这两个字典,我使用 isEqualToDictionary:方法。有关 isEqualToDictionary:的说明

I have two NSDictionaries containing NSStrings. To compare this two dictionaries I use isEqualToDictionary: method. The documentation on isEqualToDictionary:says

如果两个词典具有相同的条目数,对于给定的键,每个字典中相应的值对象满足isEqual:test。

"Two dictionaries have equal contents if they each hold the same number of entries and, for a given key, the corresponding value objects in each dictionary satisfy the isEqual: test."

所以,我的字符串由 isEqual:

So, my strings are compared by isEqual: method.

问题是:

如何 isEqual:工作 NSString

The question is:
How does isEqual: work for the NSString?

是否使用 isEqual: NSObject
我已经读过 isEqual NSObject 只是比较地址,使用 = =
为了证明或反驳这个想法,我写了一个例子:

Does it use isEqual: from NSObject?I've read that isEqual from NSObject just compares addresses, using ==.To prove or disprove this idea I wrote a sample:

NSString *str1 = @"sampleString";
NSString *str2 = [NSString stringWithFormat:@"%@", @"sampleString"];
BOOL result = [str1 isEqual:str2];

结果 YES ,但是 str1 str2 的地址不同。

所以,它不使用 isEqual: NSObject (什么?或 NSObject isEqual:执行比检查地址相等更复杂的操作。

The result is YES, the addresses of str1 and str2 are different though.
So, either it does not use isEqual: from NSObject (what than?), or NSObject's isEqual: does something more complicated then just checking equality of addresses.

有人知道它是如何工作的?

Does anybody know how does it really work?

推荐答案

NSString 覆盖 isEqual:以正确比较字符串,所以你可以这样比较字典。

NSString overrides isEqual: to properly compare strings, so you're perfectly fine to comparing dictionaries this way.

这篇关于比较包含NSStrings的NSDictionaries的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-27 01:09