如何仅在存在其他属性时调用具有条件绑定的方法



所以我想知道是否有可能调用在条件绑定中使用其他属性的方法,如果其他属性在一行中不是nil。

像这样的伪代码:

if let foo = method(prop), bar = prop {
   // do something with foo
}

只有当prop不为nil时才会调用方法(prop):

if let prop = prop, let foo = method(prop) {
    // do something with foo
}

一个解决方案是使用map:

if let foo = prop.map({ method($0) }) {
     // ...
}

最新更新