本文是蘋果swift程式語言的備忘單和參考之南,以後會涵蓋swift的所有關鍵特性,包括strings、arrays、dictionaries以及flow control。swift是蘋果在wwdc 2014上發布的適用於ios和os x平台應用的開發。(持續更新的內容,歡迎你來貼自己的備忘單)
variables
varconstantsmyint = 1
varmyexplicitint: int = 1
// explicit type
varx = 1, y = 2, z = 3
// declare multiple integers
myexplicitint = 2 // set to another integer value
let myint = 1stringsmyint = 2 // compile-time error!
varlogical operatorsmystring =
"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"
varprintingtrue
var// logical not, sad = false
varvar
let name =arrays"swift"
println("hello"
) println("my name is \(name)"
) print("see you "
) print("later"
) /* hello
my name is swift
see you later */
vardictionariescolors = [
"red"
, "blue"
] var
morecolors: string = [
"orange"
, "purple"
] // explicit type
) // [red, blue, green]
colors += "yellow"
// [red, blue, green, yellow]
colors += morecolors // [red, blue, green, yellow, orange, purple]
vardays = [
"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]
varconditionalsdays = [
"mon"
: "monday"
, "tue"
: "tuseday"
] days["tue"
] =
"tuesday"
// change the value for key "tue"
days["wed"
] =
"wednesday"
// add a new key/value pair
varmoredays: dictionary = [
"thu"
: "thursday"
, "fri"
: "friday"
] moredays["thu"
] = nil
// remove thu from the dictionary
moredays.removevalueforkey("fri"
) // remove fri from the dictionary
//if statementfor loopsif)
} else
let speed = 28
ifspeed <= 0 else
ifspeed <= 30 else
// safe speed
//switch statement
let n = 2
switch
n // it's between 2 and 4!
forwhile loopsvarindex = 1; index
forindex
in1..3
forindex
in1...3
let colors = ["red"
, "blue"
, "yellow"
] for
color
incolors
// color: red
// color: blue
// color: yellow
let days = ["mon"
: "monday"
, "tue"
: "tuesday"
] for
(shortday, longday)
indays
// mon is short for monday
// tue is short for tuesday
var原文:count = 1
while
count
// count is 1
// count is 2
count = 1
while
count
//count = 1
do while
count
// count is 1
// count is 2
count = 1
do while
count
// count is 1
swift cheat sheet
另附上raywenderlich上ray wenderlich
的備忘單。
Swift的筆記和參考
好久沒來了,趁著新語言swift發布,繼續鑽研中!create class 建立類 過載效果 create class 建立類 class myclass method 成員方法 func doit func doit int func doit a int int func doit a int,...
Apt和dpkg快速參考
apt cache search package 搜尋包 apt cache show package 獲取包的相關資訊,如說明 大小 版本等 sudo apt get install package 安裝包 sudo apt get install package reinstall 重新安裝包 ...
對比Swift和Objective C中單例的寫法
objective c networktools.h中 instancetype sharenetworktools networktools.m中 instancetype sharenetworktools return instance swift 傳統寫法 在swift中,類方法中是不允許定...