https://developer.apple.com/library/content/documentation/General/Conceptual/DevPedia-CocoaCore/ObjectComparison.html#//apple_ref/doc/uid/TP40008195-CH37-SW3

Object comparison refers to the ability of an object to determine whether it is essentially the same as another object. You evaluate whether one object is equal to another by sending one of the objects an isEqual: message and passing in the other object. If the objects are equal, you receive back YES; if they are not equal, you receive NO. Each class determines equality for its instances by implementing class-specific comparison logic. The root class, NSObject, measures equality by simple pointer comparison; at the other extreme, the determinants of equality for a custom class might be class membership plus all encapsulated values.

Some classes of the Foundation framework implement comparison methods of the form isEqualToType:—for example,  isEqualToString: and isEqualToArray:. These methods perform comparisons specific to the given class type.

The comparison methods are indispensable coding tools that can help you decide at runtime what to do with an object. The collection classes such as NSArray and NSDictionary use them extensively.

Implementing Comparison Logic

If you expect instances of your custom subclass to be compared, override the isEqual: method and add comparison logic that is specific to your subclass. Your class, for example, might accept the superclass’s determination of equality but then add further tests. Your class may have one or more instance variables whose values should be equal before two instances of your class can be considered equal. The following implementation of isEqual: performs a series of checks, ending with one that is class-specific (the name property).

- (BOOL)isEqual:(id)other {
    if (other == self)
        return YES;
    if (![super isEqual:other])
        return NO;
    return [[self name] isEqualToString:[other name]]; // class-specific
}

If you override isEqual:, you should also implement the hash method to generate and return an integer that can be used as a table address in a hash table structure. If isEqual: determines that two objects are equal, they must have the same hash value.

最新文章

  1. How to convert webp to png/jpg/gif in MacOS
  2. 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)
  3. AngularJS 使用$sce控制代码安全检查
  4. iOS之01-基本语法
  5. [LeetCode] Remove Invalid Parentheses
  6. 1763 An Essay towards solving a Problem in the Doctrine of Chances
  7. [saiku] olap数据源管理
  8. [物理学与PDEs]第1章 电动力学
  9. [51单片机]18B20驱动函数
  10. aix puppet agent
  11. ExtJS4.2学习(三)——入门基础
  12. GreenDao与Rx的完美搭配
  13. 数据挖掘_requests模块的get方法
  14. 配置VLAN
  15. 测试常用Linux命令总结
  16. P1880 [NOI1995]石子合并 区间dp+拆环成链
  17. EAS开发报错 :数据库表 或 视图 不存在
  18. R 544
  19. 服务器由于redis未授权访问漏洞被攻击
  20. Delphi 解析系统环境变量

热门文章

  1. 微信小程序小结(1) ------ 前后端交互及wx.request的简易封装
  2. 谈谈Vue/React中的虚拟DOM(vDOM)与Key值
  3. mysql隔离级别与锁,接口并发响应速度的关系(2)
  4. Mysql与Sql server,Sum函数跟Count函数
  5. 约瑟夫问题(vector的使用)
  6. HBase 相关API操练(三):MapReduce操作HBase
  7. vs2017通过snippet代码片断进行标准化注释
  8. 还不知道如何使用 IDEA ?教你三招快速掌握 IDEA
  9. win7,docker安装后,创建虚拟机分配不了ip错误 err: exit status 255
  10. springboot mybatis 自动生成代码(maven+IntelliJ IDEA)