覆盖 - (BOOL)isEqual:(id)对象

时间:2012-01-23 05:54:23

标签: iphone objective-c ios comparison nsobject

我想覆盖此方法来比较两个对象。

-(BOOL)isEqual:(id)object

调用功能

-(void)overridemethod(

custom class object1 isEqual:  custom class object2

功能定义

let suppose comparing the date parameter of two objects. Then how can i do that.

提前致谢

1 个答案:

答案 0 :(得分:10)

实施通常采用以下形式:

@interface MONInteger : NSObject
{
@private
    int value;
}

@property (nonatomic, assign, readonly) int value;

@end

@implementation MONInteger
...
- (BOOL)isEqual:(id)object
{
    // MONInteger allows a comparison to NSNumber
    if ([object isKindOfClass:[NSNumber class]]) {
        NSNumber * other = object;
        return self.value == other.intValue;
    }
    else if (![object isKindOfClass:self.class]) {
        return NO;
    }
    MONInteger * other = object;
    return self.value == other.value;
}

@end