Just some notes after reading Apple’s official online guide.
Most code are from the guide, with some modification by me.
===== Full Notes =====
Strings and Characters
String Literals
1
letsomeString="..."
Initializing an Empty String
1234567
varemptyString=""// empty string literalvaranotherEmptyString=String()// initializer syntax// these two strings are both empty, and are equivalent to each otherifemptyString.isEmpty{println("Nothing to see here")}
String Mutability
1234567
varvariableString="Horse"variableString+=" and carriage"// variableString is now "Horse and carriage"letconstantString="Highlander"constantString+=" and another Highlander"// this reports a compile-time error - a constant string cannot be modified
Strings Are Value Types
Swift’s String type is a value type. If you create a new String value, that String value is copied when it is passed to a function or method, or when it is assigned to a constant or variable. In each case, a new copy of the existing String value is created, and the new copy is passed or assigned, not the original version.
NSString in obj-c is NOT value type.
Working with Characters
123456
forcharacterin"Dog!🐶"{println(character)}// a single characterletyenSign:Character="¥"
Concatenating Strings and Characters
123456789101112
letstring1="hello"letstring2=" there"varwelcome=string1+string2// welcome now equals "hello there"varinstruction="look over"instruction+=string2// instruction now equals "look over there"letexclamationMark:Character="!"welcome.append(exclamationMark)// welcome now equals "hello there!"
String Interpolation
123
letmultiplier=3letmessage="\(multiplier) times 2.5 is \(Double(multiplier) * 2.5)"// message is "3 times 2.5 is 7.5"
Unicode Scalars
Special Unicode Characters in String Literals
12345
letwiseWords="\"Imagination is more important than knowledge\" - Einstein"// "Imagination is more important than knowledge" - EinsteinletdollarSign="\u{24}"// $, Unicode scalar U+0024letblackHeart="\u{2665}"// ♥, Unicode scalar U+2665letsparklingHeart="\u{1F496}"// 💖, Unicode scalar U+1F496
Extended Grapheme Clusters
12345678910
leteAcute:Character="\u{E9}"// életcombinedEAcute:Character="\u{65}\u{301}"// e followed by ́// eAcute is é, combinedEAcute is életprecomposed:Character="\u{D55C}"// 한letdecomposed:Character="\u{1112}\u{1161}\u{11AB}"// ᄒ, ᅡ, ᆫ// precomposed is 한, decomposed is 한letenclosedEAcute:Character="\u{E9}\u{20DD}"// enclosedEAcute is é⃝
Counting Characters
123456789101112
letunusualMenagerie="Koala 🐨, Snail 🐌, Penguin 🐧, Dromedary 🐪"println("unusualMenagerie has \(countElements(unusualMenagerie)) characters")// prints "unusualMenagerie has 40 characters"varword="cafe"println("the number of characters in \(word) is \(countElements(word))// prints "the number of characters in cafe is 4"word+="\u{301}"// COMBINING ACUTE ACCENT, U+0301println("the number of characters in \(word) is \(countElements(word))// prints "the number of characters in café is 4"
String and Character Equality
123456
letquotation="We're a lot alike, you and I."letsameQuotation="We're a lot alike, you and I."ifquotation==sameQuotation{println("These two strings are considered equal")}// prints "These two strings are considered equal"
Two String values (or two Character values) are considered equal if their extended grapheme clusters are canonically equivalent.
12345678910
// "Voulez-vous un café?" using LATIN SMALL LETTER E WITH ACUTEleteAcuteQuestion="Voulez-vous un caf\u{E9}?"// "Voulez-vous un café?" using LATIN SMALL LETTER E and COMBINING ACUTE ACCENTletcombinedEAcuteQuestion="Voulez-vous un caf\u{65}\u{301}?"ifeAcuteQuestion==combinedEAcuteQuestion{println("These two strings are considered equal")}// prints "These two strings are considered equal"
Conversely, LATIN CAPITAL LETTER A (U+0041, or “A”), as used in English, is not equivalent to CYRILLIC CAPITAL LETTER A (U+0410, or “А”).
12345678
letlatinCapitalLetterA:Character="\u{41}"letcyrillicCapitalLetterA:Character="\u{0410}"iflatinCapitalLetterA!=cyrillicCapitalLetterA{println("These two characters are not equivalent")}// prints "These two characters are not equivalent"
letromeoAndJuliet=["Act 1 Scene 1: Verona, A public place","Act 1 Scene 2: Capulet's mansion","Act 1 Scene 3: A room in Capulet's mansion","Act 1 Scene 4: A street outside Capulet's mansion","Act 1 Scene 5: The Great Hall in Capulet's mansion","Act 2 Scene 1: Outside Capulet's mansion","Act 2 Scene 2: Capulet's orchard","Act 2 Scene 3: Outside Friar Lawrence's cell","Act 2 Scene 4: A street in Verona","Act 2 Scene 5: Capulet's mansion","Act 2 Scene 6: Friar Lawrence's cell"]varact1SceneCount=0forsceneinromeoAndJuliet{ifscene.hasPrefix("Act 1 "){++act1SceneCount}}println("There are \(act1SceneCount) scenes in Act 1")// prints "There are 5 scenes in Act 1"varmansionCount=0varcellCount=0forsceneinromeoAndJuliet{ifscene.hasSuffix("Capulet's mansion"){++mansionCount}elseifscene.hasSuffix("Friar Lawrence's cell"){++cellCount}}println("\(mansionCount) mansion scenes; \(cellCount) cell scenes")// prints "6 mansion scenes; 2 cell scenes"
A collection of UTF-8 code units (accessed with the string’s utf8 property)
A collection of UTF-16 code units (accessed with the string’s utf16 property)
A collection of 21-bit Unicode scalar values, equivalent to the string’s UTF-32 encoding form (accessed with the string’s unicodeScalars property)