Is there any way to fix multiple errors at once by Adding \'@objc\' to expose this instance method to Objective-C
, I actually had over 200 of these errors, after I
You'll want to run the migrator – go "Edit > Convert > To Current Swift Syntax..." in Xcode, and select "Minimize Inference" for the "Swift 4 @objc inference" option (I actually didn't realise until recently that the most of what the migrator does is just automatically applying compiler fix-its).
If you're already in a Swift 4 target, you won't be able to run migration on it. You can fix this by just changing the "Swift Language Version" build setting to "Swift 3.2" for that target, and then running the migrator on it (which will switch the version back to 4).
After the migrator has run, you'll notice that the "Swift 3 @objc inference" build setting has been set to "On" – you'll want to test your program with this build setting to ensure you don't get any runtime warnings about Obj-C entrypoints that aren't marked as @objc
, and then change the build setting to "Default" when done, as discussed in this Q&A.
Additionally, after migration, feel free to look over the places where @objc
has been added – you may be able to neaten code up by using a single @objc extension
of the given class to define a group of methods that you want to expose to Obj-C. If you're looking at a class that requires Obj-C exposure for all its compatible members, then you can mark the class @objcMembers
. See this Q&A for more info on that.
You can use the @objcMembers attribute on your whole class or struct. You need to choose the classes on which you want to apply the @objcMembers attributes.
You can follow this tutorial.
According to Apple doc:
When a Swift class introduces many new methods or properties that require behavior from the Objective-C runtime, use the @objcMembers attribute in the declaration of that class. Applying the @objcMembers attribute to a class implicitly adds the @objc attribute to all of its Objective-C compatible members. Because applying the @objc attribute can increase the compiled size of an app and adversely affect performance, only apply the @objcMembers attribute on declarations when each member needs to have the @objc attribute applied.