Swift笔记 - 02.switch、case、where、标签语句、函数、可变参数、别名

Swift笔记 - 02.switch、case、where、标签语句、函数、可变参数、别名

switch/case复合条件匹配

switch也支持Character、String类型

let string = "Jack"
switch string {
case "Jack":
    fallthrough
case "Rose":
    print("Right person")
default:
    break
} // Right person


switch string {
case "Jack", "Rose":
    print("Right person")
default:
    break
} // Right person


let character: Character = "a"
switch character {
case "a", "A":
    print ("The letter A")
default: 
    print ("Not the letter A")
} // The letter A

switch/case的区间匹配与元组匹配

//区间匹配
let count = 62
switch count {
case 0:
    print("none")
case 1..<5:
    print("a few")
case 5..<12:
    print("several")
case 12..<100:
    print("dozens of")
case 100..<1000:
    print("hundreds of")
default:
    print("many")
} // dozens of

//元组匹配
let point = (1, 1)
switch point {
case (0, 0):
    print("the origin")
case (_, 0):
    print("on the x-axis")
case (0, _):
    print("on the y-axis")
case (-2...2, -2...2):
    print("inside the box")
default:
    print("outside of the box")
} // inside the box

switch/case的值绑定

let point = (2, 0)
switch point {
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 a y value of \(y)")
case let (x, y) :
    print ("somewhere else at (\(x), \(y))")
} // on the x-axis with an x value of 2

where 子句

let point = (1, -1)
switch point {
case let (x, y) where x == y:
    print("on the line x == y")
case let (x, y) where x == -y:
    print("on the line x == -y")
case let (x, y):
    print("(\(x),\(y))isjustsomearbitrarypoint")
} // on the line x = -y

//将所有正数加起来
var numbers = [10, 20, -10, -20, 30, -30]
var sum = 0
for num in numbers where num > 0 {//使用where来过滤num
    sum += num
}
print(sum) // 60

标签语句

outer: for i in 1...4 {
    for k in 1...4 {
        if k == 3 {
            continue outer
        }
        if i == 3 {
            break outer
        }
        print("i == \(i), k == \(k)")
    }
}

函数的隐式返回

如果整个函数体是一个单一表达式,那么函数会隐式返回这个表达式

func sum(v1: Int, v2: Int) -> Int {
    v1 + v2
}
sum(v1: 10, v2: 20)  // 30

Swift可变参数

func sum(_ numbers: Int...) -> Int {
    var total = 0
    for number in numbers {
        total += number
    }
    return total
}
sum(10, 20, 30, 40) // 100
//参数string不能省略标签
func test(_ numbers: Int..., string: String, _ other: String) { }
test(10, 20, 30, string: "Jack", "Rose")

函数类型作为函数返回值

func next (_ input: Int) -> Int {
    input + 1
}
func previous(_ input: Int) -> Int {
    input - 1
}
func forward(_ forward: Bool) -> (Int) -> Int {
    forward ? next : previous
}
forward(true) (3) // 4
forward(false)(3) // 2

typealias 取别名

typealias用来给类型起别名

typealias Byte = Int8
typealias Short = Int16
typealias Long = Int64

typealias Date = (year: Int, month: Int, day: Int)

func test(_ date: Date) {
    print(date.0)
    print(date.year)
}

test((2011, 9, 10))

按照Swift标准库的定义,void就是空元组()

public typealias Void = ()

typealias IntFn = (Int, Int) -> Int

func difference(v1: Int, v2: Int) -> Int {
    v1 - v2
}
let fn: IntFn = difference
fn(20, 10) // 10

func setFn(_ fn: IntFn) { }
setFn(difference)
func getFn() -> IntFn { difference }

嵌套函数

inline内联函数

//水远不会被内联(即使开启了编译器优化)
@inline(never) func test() {
    print("test")
}

//开启编译器优化后,即使代码很长,也会被内联(遊归调用两数、动态派发的函數除外)
@inline(__always) func test() {
    print("test")
}

版权所有,转载请注明出处 luowei.github.io.