@objc Attribute in Swift:
By default, Swift generates code that is only accessible to other Swift code. However, when you need to interact with Objective-C code or frameworks (such as UIKit), you use @objc
to explicitly instruct Swift to make certain classes, methods, properties, or other entities available to Objective-C as well as Swift code.
The @objc keyword in Swift is used to expose Swift code to Objective-C.
For example, if you want to use a Swift method with a UIBarButtonItem or a Timer, both of which are Objective-C components, you need to mark that method with @objc. This ensures that the method can be recognized and used by both Swift and Objective-C code.
Example with Timer:
let timer = Timer.scheduledTimer(timeInterval: 1.0, target: self, selector: #selector(startTimer), userInfo: nil, repeats: true) @objc func startTimer() { print("Timer started!") }
Note: Here we have to add the @objc attribute to a function that you want to use with target-action (startTimer() in this example). This will make the function available to Objective-C, which is required because UIControl (Timer, UIButton) and target-action is an Objective-C class.
What, if we not add @objc in above example?
Without @objc this code will lead to issue –
Here are some common scenarios when you might use the @objc
keyword:
1. Exposing Swift Code to Objective-C:
- If you have Swift code that needs to be accessible and usable from Objective-C code, you can use the
@objc
attribute on classes, methods, properties, or other elements that you want to expose.@objc class MySwiftClass: NSObject { @objc func mySwiftMethod() { // Swift method } }
2. Interoperability with Objective-C Frameworks:
- When working with Objective-C frameworks or APIs, you might need to use the
@objc
attribute to make your Swift code compatible.
@objc func mySwiftFunction() { // Swift function }
3. Selectors and Dynamic Dispatch:
- When you need to use selectors with methods, the
@objc
attribute allows the Swift method to be referenced by a string-based selector. This is often necessary when working with certain APIs or performing dynamic dispatch.
@objc func mySwiftMethod() { // Swift method } let selector = #selector(MySwiftClass.mySwiftMethod)
4. Inheriting from Objective-C Classes:
- When inheriting from Objective-C classes or conforming to Objective-C protocols in Swift, you might need to use the
@objc
attribute.
@objc class MySwiftClass: NSObject, SomeObjectiveCProtocol { // Swift class conforming to Objective-C protocol }
5. Dynamic Behavior and Key-Value Coding (KVC):
- The
@objc
attribute is required for classes, methods, and properties if you want to use features like Key-Value Observing (KVO) and Key-Value Coding (KVC).
@objc class MyObservableClass: NSObject { @objc dynamic var myProperty: Int = 0 }
Important Considerations:
- The
@objc
attribute comes with some runtime overhead, and its usage should be considered when needed for interoperability rather than as a default practice. - Swift classes and methods without
@objc
are not automatically exposed to Objective-C. Only elements marked with@objc
can be used from Objective-C code.
In summary, the @objc
keyword is used to make Swift code accessible from Objective-C and enables interoperability between Swift and Objective-C in mixed-language projects. Use it selectively based on the specific interoperability requirements of your project.
Read More:
Helpful article. Interview series is very helpful for beginners as well as senior level developers.