Overview:
I have an example where in certain cases Swift 4.1
allows me to refer to an ivar
in a closure without a capture list or without specifying self.ivar
Prior to Swift 4.1 it would throw a compilation error but in Swift 4.1 in some cases it does compile ok.
Code:
class Car {
var price = 100
func f1(context: NSManagedObjectContext) {
context.performAndWait {
price = 110 //Why is this not a syntactic error in Swift 4.1 ?
//So as a user how would I know which closures don't require an explicit capture
//prior to Swift 4.1, either had to capture self as weak or use strong reference of self
//Has anything changed in Swift 4.1 in this regards
//Now in Swift 4.1 it allows me to use price without explicit mention how self is captured
}
}
func f2() {
DispatchQueue.main.async {
price = 120 //Syntactic error as expected (same as prior to Swift 4.1)
//Here need to mention self.price or capture it as a weak reference.
//However in context.performAndWait it wasn't required
}
}
Questions:
- Why in some cases, how
self
is to be captured (strong / weak) need not be explicitly mentioned ? - How to know for which closures an explicit mention of how
self
is captured is not required ? - Was this something introduced in
Swift 4.1
, if so is there a proposal / article / documentation where I can read more about this ?