Is it always necessary to use [unowned self] in closures in Swift?
Hi everyone! I started learning Swift and encountered a problem with memory management in closures. I've read many articles on Stack Overflow, and everywhere they say you need to use `[weak self]` or `[unowned self]` to avoid retain cycles. But I'm confused about when exactly to use `unowned`.
Here's an example: I have a class `NetworkManager` that loads data and calls a closure upon completion.
```swift
class NetworkManager {
func fetchData(completion: @escaping (Data?) -> Void) {
DispatchQueue.global().async {
// simulate loading
sleep(2)
let data = Data()
completion(data)
}
}
}
class ViewController: UIViewController {
let networkManager = NetworkManager()
func loadData() {
No answers yet.
Log in to leave an answer