ios – 在dispatch_async中正确引用self

如何在快速关闭中正确引用自我?
dispatch_async(dispatch_get_main_queue()) {
    self.popViewControllerAnimated(true)
}

我收到错误:

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

随机我试过:

dispatch_async(dispatch_get_main_queue()) { ()
    self.popViewControllerAnimated(true)
}

它工作.不确定extra()的作用!有人在乎解释吗?谢谢!

解决方法

这与人们遇到这些问题的问题相同:

What am I doing wrong in Swift for calling this Objective-C block/API call?

animateWithDuration:animations:completion: in Swift

这是一般的想法:

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

闭包的优化之一是:

Implicit returns from single-expression closures

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

相关文章

UITabBarController 是 iOS 中用于管理和显示选项卡界面的一...
UITableView的重用机制避免了频繁创建和销毁单元格的开销,使...
Objective-C中,类的实例变量(instance variables)和属性(...
从内存管理的角度来看,block可以作为方法的传入参数是因为b...
WKWebView 是 iOS 开发中用于显示网页内容的组件,它是在 iO...
OC中常用的多线程编程技术: 1. NSThread NSThread是Objecti...