The storage mechanism for strings in Swift differs from other languages in that the characters are not stored in a fixed byte format, and each character may occupy a different number of bytes.
When we need to know how many strings a string has, we often want to know the number of characters we see, not the number of bytes stored.
A character may need to be stored 1,2,3,4 bytes.
English: 1 bytes
Xxx:2 a byte. ---didn't find an example.
Chinese: 3 bytes
Emoji expression character: 4 bytes
Charaters.count indicates how many characters, that is, we see a few characters, not care about how they are stored.
It's the same as Unicodescalars.count.
For codeunit in Language.utf8
{
print ("Utf8:\ (codeunit)", Terminator: "")
}
print ("\ n")
for Codeunit in Language.utf16
{
print ("Utf16:\ (codeunit)", Terminator: "")
}
results:
utf8:83 utf8:119 utf8:105 utf8:102 utf8:116 utf8:232 utf8:175 utf8:173 utf8:232 utf8:168 utf8:128
For ease of use, we can extend the properties of string:
Custom string length computed property
extension string
{
var length:int {return
self.characters.count
}
}}
Language.length //returns 7
Let's take a look at the official example and make it clear:
This is an example of a mixture of different types of characters, the dog head is an expression character, occupies 4 bytes.
1. UTF8 expression:
You can see 10 bytes occupied, that is, DogString.utf8.count = 10.
Each unit is a UInt8 type
2. UTF16 expression: DogString.utf16.count = = 6
Each unit is unsigned int16 type
An emoticon requires 2 UInt16 representations.
3. Unicodescalar Scalar representation
DogString.unicodeScalars.count = 5
Each unit is unicodescalar, consuming UInt32 memory and holding 21bit of data.
Each unicodescalar has a value of that returns the scalar ' s 21-bit value, represented within a uint32value.
This expression is closest to what we see.
Reference Links:
Swift_programming_language:stringsandcharacters