Swift Study Notes 04

Source: Internet
Author: User
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




Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.