Browse Questions
- An Xcode MVVM Feature template generates three pre-wired files for SwiftUI screens (Model with explicit state enum, ViewModel with injectable service protocol, and View with StateObject…
- A lazy property is only computed the first time it is accessed.
- Themis exposes Secure Cell for encrypting data at rest, Secure Message for encrypting or signing individual payloads between two parties, Secure Session for an encrypted stateful channel over an…
- 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)