Categories
Swift

String.count vs NSString.length

generally speaking,Swift inner String And it is NSString Bridged,For example, I have written String NSString and what difference?,In short we are here to discuss major,String of count and NSString of length What is the difference in the end。

String.count

String.count in reality String.Characters.count (Swift earlier versions),Swift Lane String ,Early stage count Property is simply the number of characters,with NSString it's the same,But soon joined theExtended-shaped clustercharacteristic,For example, a Emoji expression,It is twice the normal length encoding of characters,But this is [a] character visual,So it String In the count [a character]。

such as:

Note that Swift’s use of extended grapheme clusters for Character values means that string concatenation and modification may not always affect a string’s character count.

Note for the Swift Character Create and modify extension means that the string-shaped cluster of values ​​used may not always affect the number of characters in a string of statistics。

NSString.length

For the very sense of history NSString Speaking,Not so complicated,It is an array of characters,So it will only be simple to calculate the number of characters,Since the length is a two-character Emoji expression,So here,Length of 2 :

compatibility

If you pay attention to this issue,You will encounter many strange little mistake,For example, in setting the text color when the rich,It causes text to the end of the abnormal,Emoji can cause distortion and so on。Because rich text is NSAttributedString ,Its length is calculated based on the character length UTF16,Rather than after the merger [visual] characters in length:

Note the highlighted lines,Here NSRange(location: 0, length: s.count) used String of count Attributes,Should be read length 1 ,but NSMutableAttributedString UTF16 character length is calculated to do,Therefore, the length of the string should be 2 ,The result add color to half Emoji character,Content garbled output。

To convert a string NSString After obtaining length We got the right result。

Lane Swift UTF16 string length

Then,Each use,Must be clearly written ("" as NSString).length The form of it? Although write extension It is not impossible,but,In fact, we can also directly from String Gets the length of the original habitat:

discuss

String and NSString There are many similar names but the same function method,But the result may not be exactly the same but,Be careful Swift string processing,These problems tend to be reflected in some places details,Such as input method for moving a cursor API,Which moved 1 length,Yes 1 UTF16 character length,For example Emoji,Count is 2 rather than 1,If you pay attention to this detail,It will lead to some automated function calculates the error in the face of Emoji expression or some rare Chinese characters,Because it is a word on these visual symbols,But actually it uses a plurality of variable-length character length UTF16。

 

Further reading

https://stackoverflow.com/a/36268059

https://stackoverflow.com/a/29833042

https://docs.swift.org/swift-book/LanguageGuide/StringsAndCharacters.html

https://www.cnswift.org/strings-and-characters

Original article written by Gerber drop-off:R0uter's Blog » String.count vs NSString.length

Reproduced Please keep the source and description link:https://www.logcg.com/archives/3253.html

By R0uter's Blog

The non-declaration,I have written articles are original,Reproduced, please indicate the link on this page and my name。

Leave a Reply

Your email address will not be published. Required fields are marked *