Câu hỏi phỏng vấn Swift
Câu hỏi

When to use strong , weak and unowned references?

Câu trả lời

In Swift, understanding when to use strong, weak, and unowned references is crucial for effective memory management and avoiding retain cycles. Here’s a detailed explanation of each type of reference and when to use them:

Strong References

A strong reference is the default type of reference in Swift. When you create a strong reference to an object, it increments the reference count of that object by one. As long as there is at least one strong reference to an object, it will not be deallocated.

When to Use Strong References:

  • Default Case: Use strong references for most properties and variables unless you have a specific reason to use weak or unowned references.
  • Ownership: When an object should own another object, meaning it should keep the object alive as long as it exists. For example, a view controller typically has strong references to its views and models.
class HomeVC: UIViewController {
    let api = API() // Strong reference
}

Weak References

A weak reference does not increment the reference count of the object it points to. This means that the object can be deallocated even if there are weak references to it. Weak references are always optional and automatically set to nil when the object they reference is deallocated.

When to Use Weak References:

  • Avoiding Retain Cycles: Use weak references to avoid retain cycles, especially in delegate patterns and closures. For example, when a child object needs to reference its parent, but the parent already holds a strong reference to the child.
class Child {
    weak var parent: Parent? // Weak reference to avoid retain cycle
}
  • Delegates: Delegates are often weak references to avoid retain cycles between the delegate and the delegating object.
protocol MyDelegate: AnyObject {
    func didSomething()
}

class MyClass {
    weak var delegate: MyDelegate? // Weak reference to delegate
}
  • Closures: When capturing self in a closure, use [weak self] to avoid retain cycles.
api.completion = { [weak self] data, error in
    self?.handleResponse(data, error)
}

Unowned References

An unowned reference is similar to a weak reference in that it does not increment the reference count. However, unlike weak references, unowned references are non-optional and assume that the referenced object will always exist as long as the unowned reference exists. If the referenced object is deallocated and the unowned reference is accessed, it will cause a runtime crash.

When to Use Unowned References:

  • Guaranteed Lifetime: Use unowned references when you are certain that the referenced object will not be deallocated before the reference is accesse...
middle

middle

Gợi ý câu hỏi phỏng vấn

senior

When is it safe to use an unowned reference?

junior

What are Extensions used for in Swift?

expert

What is the difference between @escaping and @nonescaping Closures in Swift?

Bình luận

Chưa có bình luận nào

Chưa có bình luận nào