Reputation: 2523
What is the difference between passing [weak self]
as an argument to a closure vs passing [weak self] ()
For example :
dispatch_async(dispatch_get_main_queue()) { [weak self] in
//Some code here
}
v/s
dispatch_async(dispatch_get_main_queue()) { [weak self] () -> Void in
//Some code here
}
Upvotes: 7
Views: 381
Reputation: 539735
You do not pass [weak self] ()
as an argument to a closure.
[weak self]
is a capture list and precedes the() -> Void
in the closure expression.
The return type or both parameter list and return type can be omitted if they can be inferred from the context, so all these are valid and fully equivalent:
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()
}
The closure takes an empty parameter list ()
and has a Void
return type.
Upvotes: 9