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.

Object comparison - (BOOL)isEqual:(id)other的更多相关文章

  1. C# 泛型 Func<object, string, bool> filter

    Func<object, string, bool>是泛型,你可以先把他看成一个普通类型,比如stringpublic class Func{ } // 自定义个普通类. Func fil ...

  2. - (BOOL)setResourceValue:(id)value forKey:(NSString *)key error:(NSError **)error

    如果我们的APP需要存放比较大的文件的时候,同时又不希望被系统清理掉,那我么我们就需要把我们的资源保存在Documents目录下,但是我们又不希望他会被iCloud备份,因此就有了这个方法 [URL ...

  3. iOS判断对象相等 重写isEqual、isEqualToClass、hash

    相等的概念是探究哲学和数学的核心,并且对道德.公正和公共政策的问题有着深远的影响. 从一个经验主义者的角度来看,两个物体不能依据一些观测标准中分辨出来,它们就是相等的.在人文方面,平等主义者认为相等意 ...

  4. NSDictionary实现原理-ios哈希hash和isEqual

    NSDictionary实现原理-ios哈希hash和isEqual   OC中自定义类的NSCopying实现的注意事项(isEqual & hash实现) http://blog.csdn ...

  5. 重载hash与isEqual:方法

    重载hash与isEqual:方法 前言 NSObject 自带了hash与isEqual:方法,服务于具有hash表结构的数据结构.NSObject自带的hash函数相当于hash表中的f(key) ...

  6. iOS开发 之 不要告诉我你真的懂isEqual与hash!

    目录 为什么要有isEqual方法? 如何重写自己的isEqual方法? 为什么要有hash方法? hash方法什么时候被调用? hash方法与判等的关系? 如何重写自己的hash方法? 为什么要有i ...

  7. iOS isEqual

    如何重写 hash 方法 一个合理的 hash 方法要尽量让 hash 表中的元素均匀分布,来保证较高的查询性能. 如果两个对象可以被视为同一个对象,那么他们的 hash 值要一样. mattt 在文 ...

  8. Objective-c中 isEqual ,isEqualToString , == 三者的区别

    首先 OC中的对象都是用指针表示,方法的调用是基于消息机制实现,== 比较的自然是指针指向的地址 然后 说下 isEqual 和 isEqualToString 的区别 IsEqual 是 NSObj ...

  9. Swift equality

    最后更新: 2017-07-23 在程序开发中,我们时常需要来判断两个对象是否相等.在编程思想中,两个对象相等指的是在内存中的地址相同,也就是两个指针指向同一个地址.但是在日常理解中,只要两个对象的内 ...

随机推荐

  1. 51nod1489(dfs)

    题目链接:https://www.51nod.com/onlineJudge/questionCode.html#!problemId=1489 题意:中文题诶- 思路:dfs 首先我们要通过攻击第1 ...

  2. 排序工作量之新任务(SHOI2001)

    排序工作量之新任务(SHOI2001) 给出两个整数n和t,求n的全排列中逆序对数为t的个数,和逆序对数为t的字典序最小全排列. 首先第一个问题可以用dp解决,\(f[i][j]\)表示前i个数,j个 ...

  3. [Xcode 实际操作]四、常用控件-(13)使用UIWebView控件加载网页

    目录:[Swift]Xcode实际操作 本文将演示网页视图的使用. 在项目导航区,打开视图控制器的代码文件[ViewController.swift] import UIKit class ViewC ...

  4. 使用mount命令挂载CDROM

    Linux显示所有的目录都在一个目录树下,而于他们位于哪一个驱动器/硬件无关.在Linux下的磁盘内容作为子目录形式出现的.可移动介质的内容不会自动出现在这些自目录的,我们必须通过挂载驱动器来实现. ...

  5. Java基础--常用API--集合类相关API

    一.泛型 1.泛型的本质是将类型参数化,即将数据类型作为参数. 2.泛型可以在编译期进行检查,其所有的强制转换是隐式且自动的,提高了代码的安全性. 3.泛型可以用在类(泛型类).接口(泛型接口).方法 ...

  6. 2018年12月30&31日

    小结:昨天由于做的题目比较少,所以就和今天写在一块了,昨天学习了差分约束和树上差分,当然树上差分是用线段树来维护的,今天重点整理了博客\(233\),然后做了几个题. 一. 完成的题目: 洛谷P327 ...

  7. docker镜像的分层结构三

    docker的镜像分层 docker里的镜像绝大部分都是在别的镜像的基础上去进行创建的,也就是使用镜像的分层结构. 实验 比如说使用dockerfile去创建一个最简单的hello镜像.创建好对应的d ...

  8. servlet获取并存储web.xml中context-param参数

    在web.xml中定义了context-param,一般不会随意改动,所以在监听器中做一次处理,容器启动时读取并存储在Properties中,方便以后取值. SysProperties 类用于存储 c ...

  9. PartTime__学习辅助软件_20161025

    1.http://www.680.com/ruanjian/412629.html 1.1.http://www.gysjxjy.com/ 密码:我的名字的拼音(全小写) 使用的身份证信息:http: ...

  10. 关于String的split方法

    在做剑指offer的时候,有一道替换空格的题,立刻就想到用这个split方法来做,但发现,这个方法会丢掉字符串最后的空格??? 百度后,知道原因,这里直接复制粘贴了: 在使用java中的split按照 ...