Is there a difference between NSString compare: and isEqual(ToString):?

不羁岁月 提交于 2019-12-01 04:14:06

Reading the docs, the only differences I can find that you haven't already mentioned are:

  1. isEqualToString: first compares the id of the two strings, which is a potential speed gain in an application that makes frequent re-use of strings. From the NSString reference:

    Return Value:
    YES if aString is equivalent to the receiver (if they have the same id or if they are NSOrderedSame in a literal comparison), otherwise NO.

  2. isEqualToString: is really more analogous to compare: options:NSLiteralSearch, as can be seen in the above quote. NSLiteralSearch is more finicky about Unicode character representation:

    “Literal” when applied to string comparison means that various Unicode decomposition rules are not applied and Unicode characters are individually compared. So, for instance, “Ö” represented as the composed character sequence “O” and umlaut would not compare equal to “Ö” represented as one Unicode character.

This is really just nitpicking compared with the false positives and undefined behavior mentioned in your question.

Source: NSString Class Reference

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!