When to use objc in Swift?
Data Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.
Swift is a powerful and modern programming language designed for iOS, macOS, watchOS, and tvOS. While Swift is both powerful and expressive, there are moments when you need to interact with Objective-C, the predecessor to Swift for Apple’s platforms. Understanding when and how to use `@objc` in Swift is critical, especially for interoperability with Objective-C codebases.
The Purpose of `@objc`
The `@objc` attribute can be used to indicate that a Swift declaration can be accessed from Objective-C code. This is essential for using Swift within mixed-language apps or legacy projects that still include significant amounts of Objective-C code.
When to Use `@objc`
Here are some situations where using `@objc` in Swift is appropriate:
- Interacting with Objective-C APIs:
- If you want to use Cocoa APIs that are entirely written in Objective-C or expose Swift methods to Objective-C, you'll need to use `@objc`.
- Target-Action Design Pattern:
- When configuring actions for user interface controls (like a UIButton) at runtime, methods need `@objc` as they're stored in an Objective-C runtime's table.
- Example:
- The `@objc` modifier is required when using dynamic dispatch or reflection, as Swift doesn’t have built-in support for these features without the Objective-C runtime.
- Example of `@objc` with `@objcMembers`:
- Properties that need to be observed using KVO should be marked with `@objc` or explicitly declared with the `dynamic` modifier.
- When an Objective-C class must call a Swift delegate or a delegate method, these methods should be marked with `@objc`.
- @objcMembers: If several members of a class need to be exposed to Objective-C, you can mark the class with `@objcMembers` rather than adding `@objc` to every method.
- dynamic: Tells the Swift runtime to use dynamic dispatch. Normally, Swift uses static dispatch for performance but using `@objc` permits method overriding at runtime due to Objective-C's dynamic nature.
- optional: For protocol methods intended to be optional, mark the protocol as `@objc`.
- Dynamic dispatch introduces subtle overhead due to runtime lookups.
- Classes utilizing `@objc` directly or through directives like `@objcMembers` must derive from `NSObject`.
- Cannot expose Swift-specific features like generics directly to Objective-C.
Related reading
- When to use PNG or JPG in iPhone development?
- When to use RxJava in Android and when to use LiveData from Android Architectural Components?
- When to use Storyboard and when to use XIBs
- When to use UICollectionView instead of UITableView?
- Where and how to populate my tab using fragment
- Where are iOS simulator screenshots stored?
- Where can I find Android source code online?
- Where do alpha testers download Google Play Android apps?
.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 courseNo course covers this one yet
Basis Lab writes one for you from a sentence about what you want to be able to do, then teaches it and checks you understood. Your first course is free.
Build my courseData Structures & Algorithms practice on Codemia
Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.