Difference between isequal and =

Source: Internet
Author: User

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.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.