Swift
Swift 4
objc
selector
deprecation

How can I deal with objc inference deprecation with selector in Swift 4?

Master System Design with Codemia

Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.

Understanding @objc

Inference Deprecation in Swift 4

Swift 4 has introduced many improvements and enhancements, focusing on making the language more consistent and expressive. However, one of the changes that may affect your code and require you to make adjustments is the deprecation of the automatic inference of @objc for members that use #selector() . This change can be a bit perplexing if you are upgrading from previous versions of Swift, especially if your codebase relies heavily on Objective-C interoperability. Let's explore what @objc inference is, why it was deprecated, and how you can adapt your Swift code to this updated behavior.

Technical Background

What is @objc

?

In Swift, the @objc attribute allows your Swift code to be exposed to Objective-C runtime. This is essential for interacting with Objective-C APIs, such as those provided by UIKit, or for using features that rely on the Objective-C runtime, like KVO (Key-Value Observing) and selectors.

The Role of #selector()

The #selector() syntax is a way to refer to a method functionally, allowing Swift to create a typed pointer to the method. This feature is extensively used for setting up actions for UI elements or specifying methods to call at particular runtime events.

Automatic @objc

Inference

In earlier versions of Swift, the compiler would automatically infer @objc when you used #selector() on a class method, assuming it might be utilized in an Objective-C context. Automatic inference was convenient but could sometimes lead to performance overheads and larger binary sizes because of extra metadata being generated for Swift-only interfaces.

What Changed in Swift 4?

Deprecation of Automatic Inference

To make Swift's Objective-C interoperability more consistent and explicit, automatic inference of @objc from #selector() has been deprecated in Swift 4. Now you need to explicitly declare a method as @objc if you intend to reference it with #selector() or use it with Objective-C APIs.

Impact on Existing Code

If you rely on automatic inference in your Swift code, you will need to add @objc explicitly to ensure the methods can be called from Objective-C or referenced with #selector() .

Adapting Your Swift Code

Before Deprecation

Here is an example of code before the deprecation:

  • Explicitness: Adding @objc increases transparency about which members are exposed to the Objective-C runtime.
  • Performance: Limiting @objc usage can minimize unnecessary metadata, potentially improving performance.
  • Maintenance: It becomes easier to manage code that interacts with Objective-C, as @objc visibility is now explicit.
  • **Use of @objcMembers **: For classes where multiple methods need @objc , consider the @objcMembers attribute to avoid repetitive @objc annotations.
  • Automatic Objective-C Compatibility: If your entire class is supposed to interact with Objective-C, mark the entire class with @objc .

Course illustration
Course illustration