1

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 done fixed about 70 errors, got tired sick if doing this one by one.

I tried selecting multiple of these errors in issue navigator but dont see a way of Fix at once.

The only way I see is to select one error at a time and hit Fix, ooh this is sickening, please help if you know any way.

enter image description here

AamirR
  • 11,672
  • 4
  • 59
  • 73

2 Answers2

2

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.

Hamish
  • 78,605
  • 19
  • 187
  • 280
  • Very helpful, the target was already moved to Swift 4, so changing the "Swift Language Version" build setting to "Swift 3.2" and running migrator worked, thanks! – AamirR Nov 18 '17 at 19:14
0

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.

vishalwaka
  • 89
  • 1
  • 12