Swift Tech World in Chinese

Swift 2 The String string

Swift provides a high-performance、Compatible with Unicode strings to achieve,It has also become a part of its own standard library。In Swift 2 in, String Type no longer comply with CollectionType protocol,Time String Yes Character Collection types,Like array。Now String Providing an open view of the character setcharacterAttributes。 Why is there such a change? String as a collection of characters, String Type of behavior andArraysetas well asdictionaryIt has a very big difference,While this may seem very common sense model。In fact it has been the case,However, due to Swift 2 Some extensions add protocol led to some necessary changes on its fundamental。

It is different from the sum of its parts

When you add an element to a collection of them,We expect this collection to accommodate this element。If you add a value to the end of the array,Array will hold this value。Collection and the dictionary is like this。In short,When you add to the end of the string to a combination of symbols,String content will automatically change。 Suppose we set a stringcafe,That is four characters c , a , f , e :

If you add a character tone "at the end of the string The+0301 "That is the symbol" ´ "WordStill has a string of four characters,However, the last character has become "It is

StringcharacterProperties no longer contains the original lowercase letters " e ” ,We do not contain added tone symbol " ´ ”。Now it contains a string with a tone symbol "´"Lowercase" It is ” :

If we are like a string of other collections treat,So for example, the uicolor.redColor() and uicolor.greenColor() Add to a collection of the collection and then there uicolor.yellowColor() The surprising。

Judgment based on the content of character

Another collection of strings and different places is how they define equality。

  • Only two arrays also have the same length,andeachYes, that's rightcorrespondingindexOfficeofelementWhen will equal equal。
  • Only two sets also have the same length,And each element is when will both contain equal。
  • Two dictionaries only they have the same set of keys will be equal。

In short, String It is defined based on standardized equal equal。If the characters have the same meaning and shape language,We say that they are equal standardization,Even if they are made of different background composition is the same Unicode。 For example, the Korean writing system consists of 24 symbols or tone composition,On behalf of individual consonants and vowels。Only time will write them consisting of readable characters。 For chestnut terms,符号 "a"([ga])By the character "ᄀ" ([g]) And "ᅡ" [a]composition。Swift language in which,They no longer consider the string is equal to or exploded by a composite character sequence:

And again,This behavior Swift has a huge collection of any differences。With a ? and ?The array is treated as ?It will become equal surprising。

Depending on your point of view

String is not a collection。But it does still provide CollectionType View:

If we just put the chestnuts "café" to take over,Decomposition of a composite character sequence composition [ c, a, f, e ] And also [ ´ ],Here is the view of the various types of string,It shows their composition:

  • characterProperties of the user perceived character of the text (for example, c, a, f, And é) is divided intoExtended-shaped cluster 。Because the string must traverse the entire string in each position (each position is called a code point) to determine the character boundaries,Performs a linear access this property 0(n)time。In dealing with those strings contain human-readable text,For example, those localizedStandardCompare(_:) Methods and localizedLowercaseString Property and other senior regional distinction should be wired by Unicode character processing algorithms。
  • Unicode scalar It disclosed the existence of a string property values ​​at the basis of standard。If the original string of characters using pre-packaged " é "rather than separate" e + ´”,This amount will be reflected in the Unicode standard view among。When you perform low-level operations for character data,Please use this API。
  • utf8andutf16Respectively, in order to support UTF-8 and UFT-16 representation and provide code points。These values ​​correspond to the actual write to the file when the particular coding。UTF-8 code units used in many of the POSIX string processing API,The UTF-16 indicates the string length and offset in Cocoa and Cocoa Touch frameworks。

More on Swift programming language content and character strings,please read The Swift Programming Language as well as Swift Standard Library Reference.

The content of the article translated from Apple's official blog Strings in Swift 2 ,Department whom I learn,Limited level,If something wrong Guestbook treatise。

Original article written by Gerber drop-off:R0uter's Blog » Swift 2 The String string

Reproduced Please keep the source and description link:

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 *