1. during actual compilation, the swift compiler optimizes the use of strings so that the actual replication only occurs when absolutely necessary, this means that when you use a string as a value type, you can achieve extremely high performance.
2、for character in "Dog!" { println(character)} // D // o // g // !
3. You can create an independent character constant or variable by marking a character type annotation and assigning a value using the character literal:
Let yensign: character = "¥"
4. Calculate the number of characters
Let users = "Koala, snail Il, Penguin, Dromedary" println ("unusualmenagerie has \ (countelements (unusualmenagerie) characters") // print the output: "audio has 36 characters
5. prefix/suffix
Prefix: Scene. hassuffix ("Capulet's mansion")? Suffix: Scene. hassuffix ("Friar Lawrence's cell ")
6. Array Operations
Add an element based on the index: shoppinglist. insert ("maple syrup", atindex: 1) corresponding to delete: Let maplesyrup = shoppinglist. removeatindex (1) remove the last item: Let apples = shoppinglist. removelast ()
7. Use the global function enumerate to traverse and obtain the subscript.
for (index, value) in enumerate(shoppingList) { println("Item \(index + 1): \(value)")}
8. Initialize an array of the specified size:
VaR threedoubles = double [] (count: 3, repeatedvalue: 0.0) // threedoubles is a double [] array, equal to [0.0, 0.0, 0.0] var anotherthreedoubles = array (count: 3, repeatedvalue: 2.5) // anotherthreedoubles is inferred as double [], and equals [2.5, 2.5, 2.5]
9. The Dictionary Update returns a string?
if let oldValue = airports.updateValue("Dublin Internation", forKey: "DUB"){println("The old value for DUB was \(oldValue).====dic\(airports)")}
10. Remove dictionary elements, same as removevalueforkey
Airports ["APL"] = nil // APL is now removed
11. Convert the dictionary key to an array.
let airportCodes = Array(airports.keys)
12. In the background, swift arrays and dictionaries are implemented by generic sets. For more information about generics and collections, see generic.
13. The content of an unchangeable dictionary cannot be changed after it is set for the first time. Immutable rows are a little different for arrays. Of course we cannot try to change the size of any immutable array, but we can reset the value corresponding to the existing index. It is a good habit to create an unchangeable array without changing the array size. In this way, the swift compiler can optimize the set we created.
July 02, 2014