swift 通过枚举匹配的可选值(模式)

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了swift 通过枚举匹配的可选值(模式)相关的知识,希望对你有一定的参考价值。

import Foundation

let x: String? = nil
let y: String? = "Hello Y"

switch (x,y) {
    case (.some(let a), .some(let b)):
        print("So \(a) and \(b)")
    case (.none, .none):
        print("None")
    case (.none, .some(let a)):
        print("One val \(a)")
    default:
        print("default")
}

// same can be written as when we need both to be non nil.

switch (x, y) {
    case let (a?, b?):
        print("So \(a) and \(b)")
    default:
        print("default")
}

以上是关于swift 通过枚举匹配的可选值(模式)的主要内容,如果未能解决你的问题,请参考以下文章