【发布时间】:2016-02-09 12:19:35
【问题描述】:
我正在尝试一些带有可选链接和两个问号的东西,但我无法理解一件事。我有这部分代码:
if let delegate = self.delegate {
delegate.webViewControllerDidCancel()
}
else {
self.navigationController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
}
这很好。完全符合我的要求。如果有人符合协议,请拨打webViewControllerDidCancel,否则只需拨打dismissViewController。
但是,我尝试以这种方式做同样的事情
self.delegate?.webViewControllerDidCancel() ?? self.navigationController?.presentingViewController?.dismissViewControllerAnimated(true, completion: nil)
这对我来说似乎合法并且也有效,但 XCode 给了我一个黄色感叹号,说
调用 '??' 的结果没用过
我想不通。这是 ??甚至应该以这种方式工作?
【问题讨论】:
-
如果
–webViewControllerDidCancel返回void或其他不可评估的东西,这似乎不合法。 -
是的,它是无效的。所以这可能是警告的原因。无论如何,最终使用了更长的版本,但我必须说这也有效(不确定副作用)