Sam*_*ain 7 xcode closures weak ios swift
[weak self]作为一个参数传递给一个闭包与传递有什么区别[weak self] ()
例如 :
dispatch_async(dispatch_get_main_queue()) { [weak self] in
//Some code here
}
Run Code Online (Sandbox Code Playgroud)
V/S
dispatch_async(dispatch_get_main_queue()) { [weak self] () -> Void in
//Some code here
}
Run Code Online (Sandbox Code Playgroud)
您不[weak self] ()作为参数传递给闭包.
[weak self]是一个捕获列表,并在之前() -> Void在闭包表达式中.
如果可以从上下文中推断出返回类型或者参数列表和返回类型,则可以省略它们,因此所有这些都是有效且完全等效的:
dispatch_async(dispatch_get_main_queue()) { [weak self] () -> Void in
self?.doSomething()
}
dispatch_async(dispatch_get_main_queue()) { [weak self] () in
self?.doSomething()
}
dispatch_async(dispatch_get_main_queue()) { [weak self] in
self?.doSomething()
}
Run Code Online (Sandbox Code Playgroud)
闭包采用空参数列表()并具有Void
返回类型.
| 归档时间: |
|
| 查看次数: |
308 次 |
| 最近记录: |