Simple Values
陣列、字典 給予空值
宣告變數
Control Flow
??
A = B ?? C B!=nil , A=B;B=nil , A=C
跟三元運算式很像,會變成 A = (B!=nil) ? B : C
let nickName: String? = nil
let fullName: String = "John Appleseed"
let informalGreeting = "Hi \(nickName ?? fullName)"
for in (for each)
let individualScores = [75, 43, 103, 87, 12]
var teamScore = 0
for score in individualScores {
if score > 50 {
teamScore += 3
} else {
teamScore += 1
} }
print(teamScore)
..範圍
for i in 0..<4 {} , i 範圍 0到3
for i in 0...4 {} , i 範圍 0到4
switch不用加break
Functions and Closures
參數
呼叫function都要帶參數名字
如果有加 _ , 就不用加名字
func greet(person: String, day: String) -> String {
return "Hello \(person), today is \(day)."
}
greet(person: "Bob", day: "Tuesday")
或
func greet(_ person: String, day: String) -> String {
return "Hello \(person), today is \(day)."
}
greet("Bob", day: "Tuesday")
縮寫
可以縮寫是因為When a closure’s type is already known, such as the callback for a delegate, you can omit the type of its parameters, its return type, or both. Single statement closures implicitly return the value of their only statement.
numbers.map({
(number: Int) -> Int in
let result = 3 * number
return result
})
或
let mappedNumbers = numbers.map({ number in 3 * number })
!不懂!
var numbers = [20, 19 , 7 , 25]
次數從哪裡來?為什麼升幕跟降幕差一次?
0 意見:
張貼留言