Argument of '#selector' refers to instance method that is not exposed to Objective-C | Add '@objc' to expose this instance method to Objective-C - Swift 4 Tutorials W3Schools

Hot

Post Top Ad

28 Oct 2017

Argument of '#selector' refers to instance method that is not exposed to Objective-C | Add '@objc' to expose this instance method to Objective-C

After installing Xcode 9 and migrating to Swift 4 from Swift 3 , @objc inference warning comes like below:

The use of Swift 3 @objc inference in Swift 4 mode is deprecated. Please address deprecated @objc inference warnings, test your code with “Use of deprecated Swift 3 @objc inference” logging enabled, and then disable inference by changing the “Swift 3 @objc Inference” build setting to “Default” for the “AppName” target.


If you introduce new methods or variables to a Swift class, marking them as @objc exposes them to the Objective-C run time. This is necessary when you have Objective-C code that uses your Swift class, or, if you are using Objective-C-type features like Selectors.

Fix compiler Errors:

We can fix this @objc inference warning by using two ways.

Solution 1:

One is to use @objc on each function or variable that needs to be exposed to the Objective-C run time as follow:

@objc func getSomeData() {

}

This is the best way for converting your code so that compiler doesn't complain.

Solution 2:

Second one is to add @objcMembers by a Class declaration as follow:

@objcMembers
class Photo {

}

This will automatically add @objc to ALL the functions and variables in the class.

This is a easy way but it increases the application size by exposing functions that did not need to be exposed.

Official Documentation

1 comment:

  1. I would like to thank you for the efforts you have made in writing this article, Its good and Informative.
    ios app development course

    ReplyDelete

Post Top Ad