Object comparison - (BOOL)isEqual:(id)other
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 isEqualTo
Type:
—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.
最新文章
- How to convert webp to png/jpg/gif in MacOS
- 使用Hive或Impala执行SQL语句,对存储在Elasticsearch中的数据操作(二)
- AngularJS 使用$sce控制代码安全检查
- iOS之01-基本语法
- [LeetCode] Remove Invalid Parentheses
- 1763 An Essay towards solving a Problem in the Doctrine of Chances
- [saiku] olap数据源管理
- [物理学与PDEs]第1章 电动力学
- [51单片机]18B20驱动函数
- aix puppet agent
- ExtJS4.2学习(三)——入门基础
- GreenDao与Rx的完美搭配
- 数据挖掘_requests模块的get方法
- 配置VLAN
- 测试常用Linux命令总结
- P1880 [NOI1995]石子合并 区间dp+拆环成链
- EAS开发报错 :数据库表 或 视图 不存在
- R 544
- 服务器由于redis未授权访问漏洞被攻击
- Delphi 解析系统环境变量
热门文章
- 微信小程序小结(1) ------ 前后端交互及wx.request的简易封装
- 谈谈Vue/React中的虚拟DOM(vDOM)与Key值
- mysql隔离级别与锁,接口并发响应速度的关系(2)
- Mysql与Sql server,Sum函数跟Count函数
- 约瑟夫问题(vector的使用)
- HBase 相关API操练(三):MapReduce操作HBase
- vs2017通过snippet代码片断进行标准化注释
- 还不知道如何使用 IDEA ?教你三招快速掌握 IDEA
- win7,docker安装后,创建虚拟机分配不了ip错误 err: exit status 255
- springboot mybatis 自动生成代码(maven+IntelliJ IDEA)