Is key-value observation KVO available in Swift?
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.
Introduction
Yes, KVO is available in Swift, but it is not a general-purpose feature for every Swift type. KVO comes from the Objective-C runtime, so it works best with NSObject-based classes and properties that are exposed to Objective-C dynamic dispatch. That is why some Swift code supports KVO cleanly while pure Swift structs and many value types do not.
KVO Works Through Objective-C Runtime Features
To make a Swift property observable by KVO, the observed object usually needs to inherit from NSObject, and the property needs Objective-C-compatible dispatch.
The @objc dynamic combination is what makes KVO possible here. Without it, the property may compile and work normally in Swift, but not participate in KVO.
Observe with Swift Key-Path API
Modern Swift KVO usually uses the typed observe API instead of the older string-based Objective-C interface.
The returned observation token must be kept alive. If it is deallocated immediately, observation stops.
What Does Not Work Well
KVO is not a universal observation system for native Swift types. It does not naturally apply to:
- structs
- enums
- many pure Swift classes without
NSObject - stored properties that are not exposed to Objective-C runtime dispatch
That is why developers sometimes think "KVO is broken in Swift" when the real issue is that they are using a type model outside KVO's design.
KVO Is Still Useful, but It Is Not the Only Option
KVO remains relevant when integrating with Cocoa APIs and existing Objective-C-based frameworks. It is especially common with Foundation and AppKit or UIKit classes that were designed around observation patterns long before Swift existed.
For new app architecture, though, Swift often offers cleaner alternatives:
- property observers such as
didSet - delegates and callbacks
- Combine publishers
- SwiftUI state bindings
Those patterns are often easier to reason about in all-Swift code.
Common Memory and Lifecycle Concerns
Observation only works as long as the observer token exists. In a controller or view model, store the token in a property:
This avoids the common mistake of creating an observation inline and then losing it immediately.
Choose KVO Only When It Fits the Boundary
If you are interacting with Objective-C framework classes, KVO can be the right tool. If you are designing a fresh Swift module, forcing KVO into the model layer is often a sign that another observation mechanism would be cleaner and more type-safe.
The key point is not "Swift has KVO" or "Swift does not have KVO". The key point is that Swift can use KVO where Objective-C runtime participation exists.
Common Pitfalls
- Trying to use KVO on structs or other pure value types.
- Forgetting
@objc dynamicon properties meant to be observed. - Not inheriting from
NSObjectwhen using classic KVO patterns. - Dropping the
NSKeyValueObservationtoken and accidentally stopping observation. - Using KVO in brand-new Swift code when simpler native observation tools would be clearer.
Summary
- KVO is available in Swift, but mainly through
NSObjectand Objective-C runtime support. - Use
@objc dynamicon observed properties that should participate in KVO. - Prefer the Swift key-path
observeAPI over old string-based observation. - Keep the observation token alive for as long as observation should continue.
- For pure Swift designs, consider delegates, property observers, Combine, or SwiftUI state instead.
Related reading
- Is my app or its dependencies violating the Android Advertising Id policy?
- Is Safari on iOS 6 caching $.ajax results?
- Is Swift Pass By Value or Pass By Reference
- Is SwiftUI backwards-compatible with iOS 12.x and older?
- Is there a good charting library for iPhone?
- Is there a method to blur a background in SwiftUI?
- Is there a reason that Swift array assignment is inconsistent neither a reference nor a deep copy?
- Is there a UIView resize event?
.png&w=3840&q=75)
Tackling System Design Interview Problems
A short course that equips you with the skills to approach system design interviews methodically.
Start the free courseTrack what you have practised
A free account saves your progress, solutions and study plan across every problem on Codemia.
Interview Questions practice on Codemia
Over 8,000 real interview questions from top companies, searchable by company and role.