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 andArray、setas 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 string “ cafe ”,That is four characters c , a , f , e :
1 2 3 4 5 6 |
var letters: [Character] = ["c", "a", "f", "e"] var string: String = String(letters) print(letters.count) // 4 print(string) // cafe print(string.characters.count) // 4 |
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 ”
1 2 3 4 5 |
let acuteAccent: Character = "\u{0301}" // ´ COMBINING ACUTE ACCENT' (U+0301) string.append(acuteAccent) print(string.characters.count) // 4 print(string.characters.last!) // é |
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 ” :
1 2 3 |
string.characters.contains("e") // false string.characters.contains("´") // false string.characters.contains("é") // true |
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,andperYes, 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:
1 2 3 4 |
let decomposed = "\u{1100}\u{1161}" // ᄀ + ᅡ let precomposed = "\u{AC00}" // 가 decomposed == precomposed // true |
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:
- characteryescharacterCollection values,orExtended-shaped cluster.
- unicode scalaryes Unicode scalar valueThe Collection.
- utf8 yes UTF–8 Code Collection unit.
- utf16 yes UTF–16 Code Collection unit.
If we just put the chestnuts "café" to take over,Decomposition of a composite character sequence composition [ c, a, f, e ] besides [ ´ ],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 LogStudio:R0uter's Blog » Swift 2 The String string
Reproduced Please keep the source and description link:https://www.logcg.com/archives/1076.html