When calling DispatchQueue.main.async or DispatchQueue.main.sync with a call to self without capturing self, I get a compiler error:

Call to method 'asd' in closure requires explicit use of 'self' to make capture semantics explicit

Since I usually use DispatchQueue.main.async, I'm now used to solving this error by capturing self, which unfortunately doesn't seem to work with DispatchQueue.main.sync:

DispatchQueue.global().async {
    DispatchQueue.main.async { [self] in
        asd() // no warning or error
    }
    DispatchQueue.main.sync { [self] in
        asd() // warning: Call to method 'asd' in closure requires explicit use of 'self' to make capture semantics explicit; this is an error in Swift 6
    }
}

This warning only appears for DispatchQueue.main.sync and not for DispatchQueue.main.async. Why? How can I avoid having to prefix every method call with self. in this case?

0

There are 0 best solutions below