ios – 在dispatch_async中正确引用self

前端之家收集整理的这篇文章主要介绍了ios – 在dispatch_async中正确引用self前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。
如何在快速关闭中正确引用自我?
dispatch_async(dispatch_get_main_queue()) {
    self.popViewControllerAnimated(true)
}
@H_404_4@我收到错误

@H_404_4@无法将表达式的类型’Void’转换为’UIViewController’类型.

@H_404_4@随机我试过:

dispatch_async(dispatch_get_main_queue()) { ()
    self.popViewControllerAnimated(true)
}
@H_404_4@它工作.不确定extra()的作用!有人在乎解释吗?谢谢!

解决方法

这与人们遇到这些问题的问题相同: @H_404_4@What am I doing wrong in Swift for calling this Objective-C block/API call?

@H_404_4@animateWithDuration:animations:completion: in Swift

@H_404_4@这是一般的想法:

@H_404_4@来自斯威夫特书:https://developer.apple.com/library/prerelease/ios/documentation/swift/conceptual/swift_programming_language/Closures.html

@H_404_4@闭包的优化之一是:

@H_404_4@Implicit returns from single-expression closures

@H_404_4@因此,如果闭包中只有一行,则闭包的返回值会发生变化.在这种情况下,popViewController返回正在弹出的视图控制器.通过向闭包添加(),您只需将其设为2行闭包,并且返回值不再隐含!

猜你在找的iOS相关文章