I have noticed the isequal method when I look at the document again. But sometimes we use = to compare the two methods. It seems that there is no difference between them? Some people on the Internet say that = is used to compare pointers. isequal is used to compare content. In fact, this is not so accurate. I tested it using code:
NSString* str1=@"111"; NSString* str2=@"111"; if([str1 isEqual:str2]){ NSLog(@"isEqual is YES"); } if(str1==str2){ NSLog(@"== is YES"); } UIImage* img1=[UIImage imageNamed:@"img1"]; UIImage* img2=[UIImage imageNamed:@"img1"]; if([img1 isEqual:img2]){ NSLog(@"isEqual is YES"); } if(img1==img2){ NSLog(@"== is YES"); } UIImageView* imgv1=[[UIImageView alloc]initWithImage:img1]; UIImageView* imgv2=[[UIImageView alloc]initWithImage:img1]; if([imgv1 isEqual:imgv2]){ NSLog(@"isEqual is YES"); } if(imgv1==imgv2){ NSLog(@"== is YES"); }
If both nsstring and uiimage are compared, yes is returned, and yes is not returned.
Summary:
1. isequal should be a pointer to compare content, which is reflected in hashcode. if the content is the same, it does not mean that isequal returns true. For example, the content of uiimageview is img1, but isequal returns no, therefore, use isequal with caution. Instead, use derivative methods, such as isequtostring.
2. If the uiimage is not created successfully, for example, if the image does not exist, Nil is returned. At this time, isequal returns no, although the hash value is 0
3. If you want to change the default method, implement isequal: method and hash method.