我尝试在单个if let行中编写以下代码块:
if let amount = datasource?.incrementForCount?(count) {
count += amount
}
else if let amount = datasource?.fixedIncrement {
count += amount
}当我尝试这样的事情时:
if let amount = datasource?.incrementForCount?(count) || let amount = datasource?.fixedIncrement {
count += amount
}我得到了一个编译时错误。
我认为where条款在这种情况下是不可能的。
是否可以将这两条if let语句合并为一条ORed语句?
发布于 2017-02-13 21:02:11
不,您不能对两个if-let语句进行OR运算,甚至不能对带有非if-let条件的if-let语句进行or运算,因为这将完全违背if-let语句的目的。if-let确保只有在可选参数可以成功解包到变量中时,才能输入语句体。
let str:String? = nil
if let s = str || true {
//s is nil, yet you're still in the if-let body
}在上面的示例中,即使s为nil,您仍然可以输入可选的主体,在这种情况下,您仍然需要检查主体中的s是否为nil,从而使第一次if-let检查变得毫无意义。
https://stackoverflow.com/questions/34250348
复制相似问题