昨天確實(shí)很忙浇垦,沒來得及更新荣挨,補(bǔ)到今天了男韧。
書看到120頁了垦沉,越發(fā)覺得swift是一門不錯(cuò)的語言煌抒。今天抽空翻了翻swift的論壇厕倍,感覺要學(xué)的東西還有好多寡壮,一點(diǎn)一點(diǎn)進(jìn)步吧。
工作上一個(gè)挺惡心的任務(wù)快完成了况既,算是一個(gè)好消息。
筆記如下:
1 switch語句,與c和java不同棒仍,在swift中悲靴,當(dāng)匹配的case執(zhí)行完畢后莫其,程序會(huì)終止switch語句癞尚,而不會(huì)繼續(xù)執(zhí)行下一個(gè)case塊乱陡。無需顯示指定break浇揩。
例如:
let c: Character = "e"
switch c {
case "a", "e", "i", "o", "u" :
print("\(c) is a vowel")
case "b", "c":
print("\(c) is a consonant")
default:
print("\(c) is not a vowel or a consonant")
}
每一個(gè)case都必須包含至少一條語句
case “A”:
case “B’: //這樣是錯(cuò)誤的
let i: Int = 5
switch i {
case 1...3:
print("\(i) in [1, 3]")
case 4...6:
print("\(i) in [4, 6]")
default:
print("\(i) in [7, 100]")
}
let somePoint = (1, 2)
switch somePoint {
case (0, 0):
print("(0, 0) is at the origin")
case (_, 0): // _ 表示任意
print("(\(somePoint.0), 0 is on the x-axis")
case (-2...2, -2...2):
print("(\(somePoint.0), \(somePoint.1)) is inside the box")
default:
print("(\(somePoint.0), \(somePoint.1)) is outside the box")
}
不像c語言憨颠,swift允許多個(gè)case匹配同一個(gè)值。如果存在多個(gè)匹配爽彤,那么只會(huì)執(zhí)行第一個(gè)匹配,其余的case塊會(huì)被忽略适篙。
let anotherPoint = (0, 2)
switch anotherPoint {
case(let x, 0): //值綁定
print("on the x-axis with an x value of \(x)")
case(0, let y):
print("on the y-axis with an y value of \(y)")
case(let x, let y):
print("(\(x), \(y))")
}
case塊還可以使用where語句來增加額外的條件
let yetAnotherPoint = (1, -1)
switch yetAnotherPoint {
case let(x, y) where x == y:
print("x == y")
case let(x, y) where x == -y:
print("x == -y")
case let(x, y):
print("(\(x), \(y)")
}
2 函數(shù),函數(shù)的語法和python不太相同匙瘪,同時(shí)參數(shù)的傳入傳出也有特殊要求铆铆。
例如:
func testFunc() -> (first : Int, second : Int) {
return (1, 2) //(second: 1, first: 2) or (first: 1, second: 2) or (1, second: 2) or (1, first: 2)都是對(duì)的
}
let funcRes = testFunc()
func testFunc2(first first: Int, second: Int = 2) {
print("first is \(first), second is \(second)")
}
testFunc2(first: 1, second: 2)//testFunc2(second: 1, first: 2)丹喻, or testFunc2(first: 1, second:2 )都不對(duì)
testFunc2(first: 1) //默認(rèn)參數(shù),類似c++
//指定外部參數(shù)名的方式翁都,如果是第一個(gè)參數(shù)碍论,寫兩遍柄慰,從第二個(gè)參數(shù)開始鳍悠,默認(rèn)就是外部參數(shù)
//func testFunc2(first first: Int, second: Int) …
函數(shù)內(nèi)部坐搔,外部參數(shù):
內(nèi)部參數(shù):只能在函數(shù)內(nèi)部用
外部參數(shù):外部調(diào)用時(shí)指定參數(shù)名藏研,指定外部參數(shù)名后概行,調(diào)用時(shí)必須指定參數(shù)名蠢挡。
新版swift中,從第二個(gè)參數(shù)開始业踏,默認(rèn)為外部參數(shù),并且必須指定勤家。
注:swift的參數(shù)傳入順序必須和定義時(shí)完全一致腹尖。
可變參數(shù)
func findMax<T: Comparable>(numbers: T...) -> T {
var max = numbers[0]
for number in numbers {
if (max < number) {
max = number
}
}
return max;
}
let max = findMax(1, 2, 3, 2, 7, 3, 1)
print(max)
注意:函數(shù)參數(shù)默認(rèn)是常量伐脖,可以使用var聲明來修改為變量
in-out,通過in-out修飾的參數(shù)讼庇,可以被改變断凶,并在離開函數(shù)時(shí)依然有效巫俺,類似c++的引用认烁。
例如:
func mySwap<T>(inout left: T, inout right: T) {
(left, right) = (right, left)
}
var left = 2, right = 3
mySwap(&left, right: &right)
print(left, right)
北京的霧霾好像快散了介汹。
再艱難的險(xiǎn)阻也終抵擋不住夢(mèng)想前進(jìn)的步伐。
希望總是生于最絕望的時(shí)刻嘹承。
有這么好的起點(diǎn)窗价,確實(shí)沒有任何借口了叹卷。
前進(jìn)吧撼港,帶著最后的倔強(qiáng)骤竹。