Swift备忘单和快速参考(持续更新...)
来源:互联网 发布:php cli 弹出dos窗口 编辑:程序博客网 时间:2024/05/07 02:05
原始地址:http://www.cocoachina.com/applenews/devnews/2014/0604/8663.html
本文是苹果Swift编程语言的备忘单和参考之南,以后会涵盖Swift的所有关键特性,包括Strings、Arrays、Dictionaries以及Flow Control。Swift是苹果在WWDC 2014上发布的适用于iOS和OS X平台应用的开发。(持续更新的内容,欢迎你来贴自己的备忘单)
Variables
- var myInt = 1
- var myExplicitInt: Int = 1 // explicit type
- var x = 1, y = 2, z = 3 // declare multiple integers
- myExplicitInt = 2 // set to another integer value
Constants
- let myInt = 1
- myInt = 2 // compile-time error!
Strings
- var myString = "a"
- let myImmutableString = "c"
- myString += "b" // ab
- myString = myString + myImmutableString // abc
- myImmutableString += "d" // compile-time error!
- let count = 7
- let message = "There are \(count) days in a week"
Logical Operators
- var happy = true
- var sad = !happy // logical NOT, sad = false
- var everyoneHappy = happy && sad // logical AND, everyoneHappy = false
- var someoneHappy = happy || sad // logical OR, someoneHappy = true
Printing
- let name = "swift"
- println("Hello")
- println("My name is \(name)")
- print("See you ")
- print("later")
- /* Hello
- My name is swift
- See you later */
Arrays
- var colors = ["red", "blue"]
- var moreColors: String[] = ["orange", "purple"] // explicit type
- colors.append("green") // [red, blue, green]
- colors += "yellow" // [red, blue, green, yellow]
- colors += moreColors // [red, blue, green, yellow, orange, purple]
- var days = ["mon", "thu"]
- var firstDay = days[0] // mon
- days.insert("tue", atIndex: 1) // [mon, tue, thu]
- days[2] = "wed" // [mon, tue, wed]
- days.removeAtIndex(0) // [tue, wed]
Dictionaries
- var days = ["mon": "monday", "tue": "tuseday"]
- days["tue"] = "tuesday" // change the value for key "tue"
- days["wed"] = "wednesday" // add a new key/value pair
- var moreDays: Dictionary = ["thu": "thursday", "fri": "friday"]
- moreDays["thu"] = nil // remove thu from the dictionary
- moreDays.removeValueForKey("fri") // remove fri from the dictionary
Conditionals
- //IF STATEMENT
- let happy = true
- if happy {
- println("We're Happy!")
- } else {
- println("We're Sad :('")
- }
- // We're Happy!
- let speed = 28
- if speed <= 0 {
- println("Stationary")
- } else if speed <= 30 {
- println("Safe speed")
- } else {
- println("Too fast!")
- }
- // Safe speed
- //SWITCH STATEMENT
- let n = 2
- switch n {
- case 1:
- println("It's 1!")
- case 2...4:
- println("It's between 2 and 4!")
- case 5, 6:
- println("It's 5 or 6")
- default:
- println("Its another number!")
- }
- // It's between 2 and 4!
For Loops
- for var index = 1; index < 3; ++index {
- // loops with index taking values 1,2
- }
- for index in 1..3 {
- // loops with index taking values 1,2
- }
- for index in 1...3 {
- // loops with index taking values 1,2,3
- }
- let colors = ["red", "blue", "yellow"]
- for color in colors {
- println("Color: \(color)")
- }
- // Color: red
- // Color: blue
- // Color: yellow
- let days = ["mon": "monday", "tue": "tuesday"]
- for (shortDay, longDay) in days {
- println("\(shortDay) is short for \(longDay)")
- }
- // mon is short for monday
- // tue is short for tuesday
While Loops
- var count = 1
- while count < 3 {
- println("count is \(count)")
- ++count
- }
- // count is 1
- // count is 2
- count = 1
- while count < 1 {
- println("count is \(count)")
- ++count
- }
- //
- count = 1
- do {
- println("count is \(count)")
- ++count
- } while count < 3
- // count is 1
- // count is 2
- count = 1
- do {
- println("count is \(count)")
- ++count
- } while count < 1
- // count is 1
原文:Swift Cheat Sheet
另附上raywenderlich上Ray Wenderlich的备忘单。
0 0
- Swift备忘单和快速参考(持续更新...)
- Swift学习(持续更新)
- c/c++备忘(持续更新)
- Unity基础备忘(持续更新)
- linux基本命令备忘(持续更新)
- Zookeeper API 备忘(持续更新)
- python知识备忘集合(持续更新)
- 备忘(持续更新...)
- Swift小点总结(持续更新)
- Swift学习之小记(持续更新)
- Swift基础知识(持续更新)
- 一些自己的感想(以此备忘,持续更新)
- Linux Centos 命令(个人备忘持续更新)
- Android问题总结——ListView(备忘,持续更新)
- iOS --- 总结Swift中常用的全局函数和extension(持续更新中)
- 矩阵快速幂模板(持续更新)
- swift学习笔记~持续更新
- Swift问题汇总~持续更新
- Android实战技巧:ViewStub的应用
- 百度地图配合java 代码制作地图
- What is /dev/tty in UNIX?
- xCode使用插件问题
- 走进JVM,浅水也能捉鱼
- Swift备忘单和快速参考(持续更新...)
- ecshop模板文件下面的 library文件夹作用
- [iOS]在WebApp中如何使用JS调用iOS的函数
- 欣赏一下,古诗背过有点忘了呢
- 无监督特征学习和深度学习的在线教程,Andrew Ng
- Jsoup抓取页面内容
- skiplist 跳表详解及其编程实现
- 15个顶级Java多线程面试题及回答
- 我代替你的消息。