Objective-c isequal, isequaltostring, = = The difference between the three

Source: Internet
Author: User

First, the objects in OC are represented by pointers, and the invocation of the method is based on the message mechanism, = =The nature of the comparison is the address pointed to by the pointer and then the difference between isequal and isequaltostring IsEqual is the NSObject method, and Isequaltostring is the NSString method, so from the perspective of the inheritance relationship is Equaltostring is IsEqual's derivative method first put an Apple official rewrite IsEqual demo-(BOOL) IsEqual: (ID) Other {if(Other = =Self )returnYES; if(!other | |! [Other Iskindofclass:[selfclass]])   returnNO; return[self isequaltowidget:other]; }   -(BOOL) Isequaltowidget: (Mywidget *) Awidget {if(Self = =awidget)returnYES; if(! [(ID) [self name] Isequal:[awidget name]])returnNO; if(![Self data] isequaltodata:[awidget data])returnNO; returnYES; } simply say: First of all will determine whether the pointer is equal, equal directly return yes, not equal to determine whether it is a homogeneous object or non-null, empty or non-homogeneous objects directly return no, and then determine the object corresponding to the equality of the properties, if all equal, return yes so it is not difficult to understand Isequaltostring's implementation of the inside of the final explanation of the relationship between Hashcode and isequal hash and isequal: methods are declared in the NSObject protocol, and closely related to each other. The implementation of the hash method must return an integer number (Nsinterger) as the table address in the hash table structure. Two object Equality (isequal: The result of a method's judgment) means that they have the same hash value. If the hash value is the same, two objects are not necessarily equal. If your object may be contained in a collection like Nsset, you need to define a hash method, such as UIWebView and ensure that the method returns the same hash value when two objects are equal. Reference test code://1.0NSString *str1 = [NSString stringWithFormat:@"skyming"]; NSString*STR2 = [NSString stringWithFormat:@"skyming"]; NSLog (@"Address of the str1 address--%p--str2--%p", STR1,STR2); NSLog (@"= =%d", str1 = =str2); NSLog (@"isequal--%d", [str1 isequal:str2]); NSLog (@"isequaltostring--%d", [str1 isequaltostring:str2]); //2.0UIImage *image1 =[UIImage IMAGENAMED:STR1]; UIImage*image2 =[UIImage IMAGENAMED:STR2]; NSLog (@"Address of the image1 address--%p--image2--%p", Image1,image2); NSLog (@"= =%d", Image1 = =image2); NSLog (@"isequal--%d", [Image1 Isequal:image2]); //3.0Uiimageview *imageview1 =[[Uiimageview alloc]initwithimage:image1]; Uiimageview*imageview2 =[[Uiimageview alloc]initwithimage:image2]; NSLog (@"imageView1 address--%p-imageview2 address--%p", IMAGEVIEW1,IMAGEVIEW2); NSLog (@"= =%d", ImageView1 = =imageView2); NSLog (@"isequal--%d", [ImageView1 isequal:imageview2]);} Reference test results: -- A- +  +: $:38.975nsstring[2029: 60b] STR1 's address--0x15586d00--STR2 's address--0x15586a40 -- A- +  +: $:38.982nsstring[2029: 60b] = =0 -- A- +  +: $:38.988nsstring[2029: 60b] isequal--1 -- A- +  +: $:38.992nsstring[2029: 60b] isequaltostring--1 -- A- +  +: $:39.004nsstring[2029: 60b] Image1 's address--0x1558ad00--image2 's address--0x1558ad00 -- A- +  +: $:39.009nsstring[2029: 60b] = =1 -- A- +  +: $:39.013nsstring[2029: 60b] isequal--1 -- A- +  +: $:39.017nsstring[2029: 60b] ImageView1 Address--0x1558b0f0-IMAGEVIEW2 Address--0x1558ba50 -- A- +  +: $:39.022nsstring[2029: 60b] = =0 -- A- +  +: $:39.026nsstring[2029: 60b] isequal--0

Objective-c isequal, isequaltostring, = = The difference between the three

Related Article

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.