模式匹配

通配符模式
  • _:完全不关心这个值,甚至可以为nil
enum Moblie {
    case Man(name: String, age: Int, height: Int?)
    case Woman(name: String, age: Int, height: Int?)
}

let Roy = Moblie.Man(name: "Roy", age: 27, height: nil)

switch Roy {
case .Man(name: _, age: let age, height: _):
    print("-- \(age)")
default:
    break
}
  • _?:虽然这个值不使用,但是要确保是非nil的
enum Moblie {
    case Man(name: String, age: Int, height: Int?)
    case Woman(name: String, age: Int, height: Int?)
}

let Roy = Moblie.Man(name: "Roy", age: 27, height: nil)

switch Roy {
case .Man(name: _, age: let age, height: _?):
    print("-- \(age)")
default:
    break
}
值绑定模式
switch Roy {
case .Man(name: let name, age: let age, height: let height):
    print("\(name) -- \(age) -- \(String(describing: height))")
default:
    break
}

switch Roy {
case let .Man(name: name, age: age, height: height):
    print("\(name) -- \(age) -- \(String(describing: height))")
default:
    break
}
元组模式
let error = (statusCode: 404, statusMessage: "Not Found")

switch error {
case (let code, _):
    print("\(code)")
default:
    break
}
枚举用例模式
enum Result {
    case error(String)
    case image(UIImage)
    case data(Data)
}

let result = Result.error("没有找到资源")

switch result {
case let .image(image):
    print(image)
case let .data(data):
    print(data)
case .error(let err):
    print("Failed; error message is \(err)")
}
可选模式
let i:Int? = 3
// 使用可选模式匹配
if case let x? = i {
    print(x)
}

let arrayOfOptionalInts: [Int?] = [nil, 2, 3, nil, 5]
// 只匹配非 nil 的元素
for case let number? in arrayOfOptionalInts {
    print("Found a \(number)")
}


let count: Int? = 5
switch count {
case 1?:
    print("1")
case 3?:
    print("3")
case 5?:
    print("5")
case _:
    print("nil")
default:
    break
}
类型转换模式
let a: Any = 5
switch a {
case is Int:
    // 这会失败因为它的类型仍然是 `Any`
    // 错误: binary operator '+' cannot be applied to operands of type 'Any' and 'Int'
    print (a + 1)
    break
case let n as String:
    // 有效并返回 '6'
    print ("result: " + n)
default: ()
}
表达式模式
  • 范围匹配
let point = (1, 2)
switch point {
case (0, 0):
    print("(0, 0) is at the origin.")
case (-2...2, -2...2):
    print("(\(point.0), \(point.1)) is near the origin.")
default:
    print("The point is at (\(point.0), \(point.1)).")
}
其他用法
  • 在条件中使用where语句
let people = (age: 100, name: "karl")
switch people {
case (let age, _) where age > 30:
    print(age)
default:
    break
}
  • if case let
let Roy2 = Moblie.Man(name: "Roy", age: 27, height: 178)

if case let .Man(name, _, _?) = Roy2 {
    print(name)
}
  • for case let
let array: [Any?] = [1,2,nil,4,54,65,76, ""]
for item in array {
    if let element = item as? Int, element > 10 {
        print(element)
    }
}

for case let x? as Int? in array where x > 10 {
    print(x)
}
综合运用
let Roy = Moblie.Man(name: "Roy", age: 27, height: 178)
let Kelly = Moblie.Woman(name: "Kelly", age: 28, height: nil)
let Frank = Moblie.Man(name: "Frank", age: 29, height: nil)
let Arvin = Moblie.Man(name: "Arvin", age: 28, height: nil)

let moblieArray = [Roy, Kelly, Frank, Arvin]

for case let .Man(name, age, _?) in moblieArray where age < 29 {
    print(name)
}

你可能感兴趣的:(模式匹配)