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 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.

时间: 2024-12-16 17:45:28

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

- (BOOL)isEqual:(id)object

每个对象都有 - (BOOL)isEqual:(id)object这个方法 例如: /** * 常用来比较两个HWEmotion对象(地址)是否一样 * * @param other 另外一个HWEmotion对象 * * @return YES : 代表2个对象是一样的,NO: 代表2个对象是不一样 */ - (BOOL)isEqual:(HWEmotion *)other { // if (self == other) { // return YES; // } else { // retu

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

Func<object, string, bool>是泛型,你可以先把他看成一个普通类型,比如stringpublic class Func{ } // 自定义个普通类. Func filter; // 自定义个字段 public Func Filter // 属性,上个字段filter的访问器.类型为Func { get { return filter;} set { } } 不考虑Func<object, string, bool>,上段代码明白不?,不明白我在给你解释.下面说

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

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

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

NSDictionary实现原理-ios哈希hash和isEqual OC中自定义类的NSCopying实现的注意事项(isEqual & hash实现) http://blog.csdn.net/linshaolie/article/details/41494303 iOS开发 之 不要告诉我你真的懂isEqual与hash! http://m.blog.csdn.net/hx_lei/article/details/53885798 http://www.jianshu.com/p/9153

OC 重写description,isEqual方法

// 为了能用%@打印出对象的有效信息,需要重写description方法 - (NSString *)description { // 最简单的办法是将属性和值组合成键值对存放到字典中 // 这样只需要调用字典的description方法就能获取对应的描述字符串 NSDictionary *dictionary = @{@"name" : name_, @"address" : address_, @"friends" : friends_};

【IOS 开发】Object - C 面向对象高级特性 - 包装类 | 类处理 | 类别 | 扩展 | 协议 | 委托 | 异常处理 | 反射

一. Object-C 对象简单处理 1. 包装类 (1) 包装类简介 NSValue 和 NSNumber : -- 通用包装类 NSValue : NSValue 包装单个 short, int, long, float, char, id, 指针 等数据; -- NSNumber 包装类 : 用于包装 C 语言数据类型; NSNumber 方法 : -- "+ numberWithXxx :" : 将特定类型的值包装成 NSNumber; -- "- initWithX

ObjC中isEqual与==的区别

isEqual是NSObject的方法,我们常用它来判断两个object是否相等, 而==也常用来来判断两个object是否相等,那么它们有什么区别呢. 看一段代码就明白了: //MyItem.h @interface MyItem : NSObject { @private NSString *identifier; } @property (nonatomic, copy) NSString *identifier; - (id)initWithIdentifier:(NSString *)

重载hash与isEqual:方法

前言 NSObject 自带了hash与isEqual:方法,服务于具有hash表结构的数据结构.NSObject自带的hash函数相当于hash表中的f(key)函数中的key,这“唯一”的key需要用户自己产生,至于用什么算法由用户自己决定. 准备 // // Model.h // Hash // // Created by YouXianMing on 16/4/15. // Copyright © 2016年 YouXianMing. All rights reserved. // #i

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

相等的概念是探究哲学和数学的核心,并且对道德.公正和公共政策的问题有着深远的影响. 从一个经验主义者的角度来看,两个物体不能依据一些观测标准中分辨出来,它们就是相等的.在人文方面,平等主义者认为相等意味着要保持每个人的社会.经济.政治和他们住地的司法系统都一致. 对程序员来说,协调好逻辑和感官能力来理解我们塑造的'相同'的语义是一项任务.'相同的问题'(的探讨)太微妙,同时有太容易被忽视.对语义没有充分的理解就直接去实现它,可能会导致没必要的工作和不正确的结果.因此对数学和逻辑系统的深刻理解与按