Переопределить - (BOOL) isEqual: (id) объект - PullRequest
2 голосов
/ 23 января 2012

Я хочу переопределить этот метод для сравнения двух объектов.

-(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 Ответ

10 голосов
/ 23 января 2012

реализация часто принимает такую ​​форму:

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