Correct comparison of strings in objective-C

Source: Internet
Author: User

If two strings are equal, use isequaltostring: instead of just comparing the pointer values of the strings. For example:

Nsstring * thing1 = @ "Hello 5"; <br/> nsstring * thing2; <br/> thing2 = [nsstring stringwithformat: @ "Hello @ D", 5]; <br/> If ([thing1 isw.tostring: thing2]) {<br/> nslog (@ "they are the same! ") <Br/>}

And

If (thing1 = thing2) {<br/> nslog (@ "they are the same! ") <Br/>}

Is different.

This is because the = Operator only judges the pointer values of thing1 and thing2, rather than the objects they refer. Because thing1 and thing2 are different strings, the second comparison method considers them different.

Sometimes we want to check whether thing1 and thing2 are the same objects? In this case, the operator = should be used. If you want to check the equivalence (that is, whether these two strings represent the same thing), use isw.tostring :.

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.