Skip to content
  • Home
  • About us
  • Contact
  • Privacy Policy
  • Disclaimer
  • Swift Online Compiler
iOS Interview Questions and Tutorials

iOS Interview Questions and Tutorials

@objc Attribute in Swift

Posted on December 3, 2023July 23, 2024 By Sid 1 Comment on @objc Attribute in Swift

Table of Contents

Toggle
  • @objc Attribute in Swift:
    • Example with Timer:
    • 1. Exposing Swift Code to Objective-C:
    • 2. Interoperability with Objective-C Frameworks:
    • 3. Selectors and Dynamic Dispatch:
    • 4. Inheriting from Objective-C Classes:
    • 5. Dynamic Behavior and Key-Value Coding (KVC):
  • Important Considerations:
@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 –

Example Without @objc

 

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:

Attributes in Swift

Top iOS Interview Questions and Answers

Blog Tags:@objc attribute in Swift, @objc keyword

Post navigation

Previous Post: Difference between Sync and Async tasks in iOS
Next Post: Dictionary in Swift: Top 20 Interview Questions

Comment (1) on “@objc Attribute in Swift”

  1. Aisha says:
    December 3, 2023 at 4:27 pm

    Helpful article. Interview series is very helpful for beginners as well as senior level developers.

    Reply

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Most Asked iOS Interview Questions

  • Top iOS Interview Questions and Answers

Categories

  • Associated Types(7)
  • Blog
  • Dictionary in Swift(20)
  • Initializers
  • Property Wrapper
  • Singleton in Swift
  • User Defaults(4)
  • XCode 15 Errors

Recent Comments

  1. Sid on Cycle inside MyApp; building could produce unreliable results
  2. Anominous on Cycle inside MyApp; building could produce unreliable results
  3. Aisha on @objc Attribute in Swift

Recent Posts

  • Enums in Swift: Brief Explanation with Code Examples
  • Higher-Order Functions in Swift: Brief Explanation with Code Examples
  • Mutability in Structs and Classes in Swift
  • Delegate Pattern in Swift
  • resueIdentifier in Swift

DSA in Swift

  • 2D Array in Swift: Interview Questions

Copyright © 2025 iOS Interview Questions and Tutorials.

Powered by PressBook WordPress theme