Swift备忘单和快速参考(持续更新...)

原始地址:http://www.cocoachina.com/applenews/devnews/2014/0604/8663.html


本文是苹果Swift编程语言的备忘单和参考之南,以后会涵盖Swift的所有关键特性,包括Strings、Arrays、Dictionaries以及Flow Control。Swift是苹果在WWDC 2014上发布的适用于iOS和OS X平台应用的开发。(持续更新的内容,欢迎你来贴自己的备忘单)
 
Variables
  
  
  1. var myInt = 1 
  2. var myExplicitInt: Int = 1 // explicit type 
  3. var x = 1, y = 2, z = 3 // declare multiple integers 
  4. myExplicitInt = 2 // set to another integer value 
Constants
  
  
  1. let myInt = 1 
  2. myInt = 2 // compile-time error! 
Strings
  
  
  1. var myString = "a" 
  2. let myImmutableString = "c" 
  3. myString += "b" // ab 
  4. myString = myString + myImmutableString // abc 
  5. myImmutableString += "d" // compile-time error! 
  6.  
  7. let count = 7 
  8. let message = "There are \(count) days in a week" 
 
Logical Operators
  
  
  1. var happy = true 
  2. var sad = !happy // logical NOT, sad = false 
  3. var everyoneHappy = happy && sad // logical AND, everyoneHappy = false 
  4. var someoneHappy = happy || sad // logical OR, someoneHappy = true 
 
Printing
  
  
  1. let name = "swift" 
  2. println("Hello"
  3. println("My name is \(name)"
  4. print("See you "
  5. print("later"
  6. /*  Hello 
  7.     My name is swift 
  8.     See you later */ 
 
Arrays
  
  
  1. var colors = ["red""blue"
  2. var moreColors: String[] = ["orange""purple"// explicit type 
  3. colors.append("green"// [red, blue, green] 
  4. colors += "yellow" // [red, blue, green, yellow] 
  5. colors += moreColors // [red, blue, green, yellow, orange, purple] 
  6.  
  7. var days = ["mon""thu"
  8. var firstDay = days[0] // mon 
  9. days.insert("tue", atIndex: 1) // [mon, tue, thu] 
  10. days[2] = "wed" // [mon, tue, wed] 
  11. days.removeAtIndex(0) // [tue, wed] 
Dictionaries
  
  
  1. var days = ["mon""monday""tue""tuseday"
  2. days["tue"] = "tuesday" // change the value for key "tue" 
  3. days["wed"] = "wednesday" // add a new key/value pair 
  4.  
  5. var moreDays: Dictionary = ["thu""thursday""fri""friday"
  6. moreDays["thu"] = nil // remove thu from the dictionary 
  7. moreDays.removeValueForKey("fri"// remove fri from the dictionary 
 
Conditionals
  
  
  1. //IF STATEMENT 
  2. let happy = true 
  3. if happy { 
  4.     println("We're Happy!"
  5. else { 
  6.     println("We're Sad :('"
  7. // We're Happy! 
  8.  
  9. let speed = 28 
  10. if speed <= 0 { 
  11.     println("Stationary"
  12. else if speed <= 30 { 
  13.     println("Safe speed"
  14. else { 
  15.     println("Too fast!"
  16. // Safe speed 
  17.  
  18.  
  19. //SWITCH STATEMENT 
  20. let n = 2 
  21. switch n { 
  22. case 1: 
  23.     println("It's 1!"
  24. case 2...4: 
  25.     println("It's between 2 and 4!"
  26. case 5, 6: 
  27.     println("It's 5 or 6"
  28. default
  29.     println("Its another number!"
  30. // It's between 2 and 4! 
 
For Loops
  
  
  1. for var index = 1; index < 3; ++index { 
  2.     // loops with index taking values 1,2  
  3. for index in 1..3 { 
  4.     // loops with index taking values 1,2 
  5. for index in 1...3 {  
  6.     // loops with index taking values 1,2,3 
  7.  
  8. let colors = ["red""blue""yellow"
  9. for color in colors { 
  10.     println("Color: \(color)"
  11. // Color: red 
  12. // Color: blue 
  13. // Color: yellow  
  14.  
  15. let days = ["mon""monday""tue""tuesday"
  16. for (shortDay, longDay) in days { 
  17.     println("\(shortDay) is short for \(longDay)"
  18. // mon is short for monday 
  19. // tue is short for tuesday 
 
While Loops
  
  
  1. var count = 1 
  2. while count < 3 { 
  3.     println("count is \(count)"
  4.     ++count 
  5. // count is 1 
  6. // count is 2 
  7.  
  8. count = 1 
  9. while count < 1 { 
  10.     println("count is \(count)"
  11.     ++count 
  12. // 
  13.  
  14.  
  15. count = 1 
  16. do { 
  17.     println("count is \(count)"
  18.     ++count 
  19. while count < 3 
  20. // count is 1 
  21. // count is 2 
  22.  
  23. count = 1 
  24. do { 
  25.     println("count is \(count)"
  26.     ++count 
  27. while count < 1 
  28. // count is 1 
 原文: Swift Cheat Sheet

另附上raywenderlich上Ray Wenderlich的备忘单。


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值