If two objects are equal,they must have the same hash value. This last point is particularly important
if you define isEqual: in a subclass and intend to put instances of that subclass into a collection. Make sure
you also define hash in your subclass.
看下面的代码:
NSIndexPath *indexPath1 = [NSIndexPath indexPathForRow:0 inSection:0];NSIndexPath *indexPath2 = [NSIndexPath indexPathForRow:0 inSection:0];NSObject *obj1 = [[NSObject alloc] init];NSObject *obj2 = [[NSObject alloc] init];NSLog(@"NSIndexPath isEqual's Result: %d",[indexPath1 isEqual:indexPath2]);NSLog(@"NSObject isEqual's Result: %d",[obj1 isEqual:obj2]);
输出结果:
NSIndexPath isEqual’s Result: 1
NSObject isEqual’s Result: 0
NSObject的实现isEqual是comare的两个对象的地址,
而hash实现就是返回对象的地址.
NSIndexPath从NSObject继承,根据NSIndexPath isEqual输出结果,
NSIndexPath的isEqual实现应该覆盖超类的isEqual方法,NSIndexPath也会覆盖超类的哈希方法.
NSIndexPath也是符合NScopying协议的.
所以NSIndexPath可以用作NSDictionary的Key类.
总结以上是内存溢出为你收集整理的objective-c – NSDictionary的适用键全部内容,希望文章能够帮你解决objective-c – NSDictionary的适用键所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)