在Swift中,如何在switch语句中编写一个case来测试根据可选的内容切换的值,如果可选项包含nil
,则跳过大小写?
以下是我的想象:
let someValue = 5
let someOptional: Int? = nil
switch someValue {
case someOptional:
// someOptional is non-nil, and someValue equals the unwrapped contents of someOptional
default:
// either, someOptional is nil, or someOptional is non-nil but someValue does not equal the unwrapped contents of someOptional
}
如果我只是这样编写它,编译器会抱怨someOptional
没有解包,但是如果我通过添加!
来显式解包它,我当然会得到一个运行时错误时间someOptional
包含nil
。添加?
而不是!
对我来说是有意义的(根据我认为的可选链接的精神),但不会使编译器错误消失(即不会实际上解开了可选项。
答案 0 :(得分:100)
可选是这样的enum
:
enum Optional<T> : Reflectable, NilLiteralConvertible {
case None
case Some(T)
// ...
}
因此,您可以像往常一样"Associated Values"匹配模式匹配它们:
let someValue = 5
let someOptional: Int? = nil
switch someOptional {
case .Some(someValue):
println("the value is \(someValue)")
case .Some(let val):
println("the value is \(val)")
default:
println("nil")
}
如果您希望使用someValue
来自switch someValue {
case let val where val == someOptional:
println(someValue)
default:
break
}
的匹配:
switch someValue {
case let val where val == someOptional:
print("matched")
default:
print("didn't match; default")
}
对于Swift&gt; 2.0 强>
{{1}}
答案 1 :(得分:43)
从Xcode 7开始(来自beta 1发行说明),“一个新的x?
模式可以用来模式匹配选项作为.Some(x)
的同义词。”这意味着在Xcode 7中以后rintaro's answer的以下变体也会起作用:
switch someOptional {
case someValue?:
print("the value is \(someValue)")
case let val?:
print("the value is \(val)")
default:
print("nil")
}
答案 2 :(得分:2)
在 Swift 4 中,您可以使用Apple的 Optional:ExpressibleByNilLiteral 包装可选
https://developer.apple.com/documentation/swift/optional
示例
enum MyEnum {
case normal
case cool
}
一些
let myOptional: MyEnum? = MyEnum.normal
switch smyOptional {
case .some(.normal):
// Found .normal enum
break
case .none:
break
default:
break
}
无
let myOptional: MyEnum? = nil
switch smyOptional {
case .some(.normal):
break
case .none:
// Found nil
break
default:
break
}
默认
let myOptional: MyEnum? = MyEnum.cool
switch smyOptional {
case .some(.normal):
break
case .none:
break
default:
// Found .Cool enum
break
}
具有值的枚举
enum MyEnum {
case normal(myValue: String)
case cool
}
某些价值
let myOptional: MyEnum? = MyEnum.normal("BlaBla")
switch smyOptional {
case .some(.normal(let myValue)) where myValue == "BlaBla":
// Here because where find in my myValue "BlaBla"
break
// Example for get value
case .some(.normal(let myValue)):
break
// Example for just know if is normal case enum
case .some(.normal):
break
case .none:
break
default:
break
}