我是 swift 的新手,但我想我已经掌握了它的窍门。但这对我的进步来说是相当困难的。
我想做的是,当我们找不到与用户查询相关的数据时,向用户抛出一条错误消息,然后继续将他带回之前的 ViewController。
然而,我在做这件事时遇到了真正的困难。在添加操作的行上,出现以下错误:“UIViewController?”不是 Void 的子类型
let alertController = UIAlertController(title: "Oops", message: "We couldn't find any data for this title, sorry!", preferredStyle: UIAlertControllerStyle.Alert)
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
self.navigationController?.popViewControllerAnimated(true)
}))
我该怎么做呢?我错过了一些明显的事情吗?我尝试摆弄已弃用的 UIAlertView,但一无所获。
只需添加一个明确的return
闭包体内的语句:
alertController.addAction(UIAlertAction(title: "Ok", style: .Default, handler: { action in
self.navigationController?.popViewControllerAnimated(true)
return
}))
发生这种情况的原因是单个语句闭包被处理为返回值,因此编译器使用popViewControllerAnimated
,不出所料,这是一个UIViewController?
。显式的 return 语句避免了这种情况。
此行为记录在单表达式闭包的隐式返回 https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html#//apple_ref/doc/uid/TP40014097-CH11-XID_157
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)