NSString is one of the most commonly used classes, almost everyone can not leave, it is very powerful function, but it is a little mystery until the encounter to find.
Body:
There is a rangeofstring in NSString's API: This is also a common API to find in strings. I generally figure the easy, all with this, rarely used rangeofstring:option:
It was because of laziness that led me to a crash, I really remember deeply.
Next, let me give you an example:
Clearly there is a ":", the result is not found. This led me to the crash of my later code. This is not to say this crash, but to find out why, why this ":", can not find it.
Print length, unexpectedly is 2. Then why can't you find it? In a different way:
Got it!!!!!
The problem is solved, but why is it so?
This has to look at the composition of Unicode.
There are two forms of Unicode for composition: The form of synthesis and the form of decomposition.
And NSString's rangeofstring, this API support for this is the case. Rangeofstring, the default is not according to the code element to find, that is, not according to Literalsearch. Although it contains ":", however, these two characters can synthesize another character with its equivalent, so it is not found.
Some of the synthetic characters have a corresponding form in the Unicode character table, and some do not. I'm not going to give you an example. This is based on the specific rules of the various characters, such as the character + Cyrillic, you can set up a lot of strange characters.
Remind:
If you want to find characters in NSString, be sure to take option. Because NSString and Unicode still have a lot of pits.
iOS Development-Summary of common errors and warnings