Browse Questions
- XCTestExpectation pauses a test until an async condition is fulfilled or a timeout is reached.
- Combine subscriptions and NotificationCenter block observers cause leaks when the observation token is stored on self while the callback closure captures self strongly.
- A Singleton ensures only one instance of a class exists globally.
- These determine how ARC (Automatic Reference Counting) manages object lifetimes.
Answer: These determine how ARC (Automatic Reference Counting) manages object lifetimes.
strong— increments the retain count. Default for most references.weak— does NOT increment retain count. Automatically set tonilwhen the object deallocates. Must beOptional.unowned— does NOT increment retain count. Assumed to always have a value — crashes if accessed after deallocation.
Code Example:
class Owner {
var pet: Pet?
}
class Pet {
weak var owner: Owner? // avoid retain cycle
}
// unowned — use when you're certain the referenced object outlives the current one
class ViewModel {
unowned let coordinator: AppCoordinator
init(coordinator: AppCoordinator) {
self.coordinator = coordinator
}
}
When to use weak vs unowned:
weak— the reference can becomenil(delegate patterns, optional parent refs)unowned— the reference should never becomenil(e.g. child → parent with guaranteed lifetime)