Tuesday, June 20, 2017

Porting Objective-C to Swift

Jeff Johnson:

The disadvantage of the Generated Interface is that it breaks all of the existing Objective-C calls to that API. The automatic compiler API transformation does not work in reverse: when you import Swift into Objective-C, the Swift API stays the same, it doesn’t automatically become more “Objective”. So once you let the compiler do its dirty work on the API, you then have to refactor any leftover Objective-C files that use it. When you’re just beginning to port an entire Objective-C project to Swift, this can be a major pain.

One approach would be to add @obj() annotations to the Swift API to customize how it’s imported into Objective-C. Then you could preserve the existing calls in Objective-C files, as well as the existing calls in Swift files, and when everything has been ported to Swift, you can ditch the @obj(). This seems like a lot of busywork for a “temporary” solution, though[…].

[…]

However, Swift does not export global functions to Objective-C, so you can’t call global Swift functions in Objective-C code. In order to export a Swift function to Objective-C, you have to wrap it with an Objective-C object.

Comments RSS · Twitter

Leave a Comment