override isEqual Objective-C

Rav*_*ja 1 objective-c

我的课

@interface sample:NSObject{

   double x;

   double y;

}
@property double x;

@property double y;

-(sample *)initWithX: (double)x andY:(double) Y;


@implementation

@synthesize x,y

-(sample *) initWIthX: (double)x andY:(double)y{

    self = [super init];

    if(self) 
    {

      self.x = x;

      self.y = y; 

    }
     return self;

}

-(BOOL)isEqual:(id)other{

 if(other == self)
   return YES;
 if(!other || ![other isKindOfClass:[self class]])
   return NO;
 return [self isEqualToSample:other];  
}


-(BOOL)isEqualToSample:(sample *) other{

 if(self == other)    
   return YES;

 if (!([self x] ==[other x])) 
   return NO;

 if (!([self y] ==[other y]))   
   return NO;

   return YES;
}
Run Code Online (Sandbox Code Playgroud)

return [self isEqualToSample:other]; 它显示警告:'sample'可能不响应'-isEqualToSample'.return从指针生成整数而不进行强制转换.

小智 5

与警告无关,但是当覆盖时,-isEqual:您还必须覆盖hash,如果那样的[a isEqual: b][a hash] == [b hash].