Bulk fix hundreds of “#selector not exposed to Objective-C” errors in Xcode 9 or 9.1

前端 未结 2 1002
星月不相逢
星月不相逢 2021-01-23 11:56

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

2条回答
  •  时光说笑
    2021-01-23 12:16

    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.

提交回复
热议问题