使用開關滿足多個約束之一
你可以建立一個元組並使用如下所示的開關:
var str: String? = "hi"
var x: Int? = 5
switch (str, x) {
case (.Some,.Some):
print("Both have values")
case (.Some, nil):
print("String has a value")
case (nil, .Some):
print("Int has a value")
case (nil, nil):
print("Neither have values")
}