Swift - Objective-C Bridge
Part of this blog post: @dynamicCallable Part 2: Swift/ObjC Bridge.
In December we demonstrated how to use the new Swift 5
run Unix commands as Swift functions,
Today we implement our very own Swift / Objective-C bridge using the same!
Of course Swift already has Objective-C integrated on the Apple platforms,
directly supported by the compiler, as well as the associated
Yet using Dynamic Callable you can actually build something similar at the library level, and we want to show you how that would look like.
Swift also runs on Linux, but it doesn't come with the Objective-C runtime and bridging features. Using the approach shown here with either libFoundation or GNUstep you could also combine Swift and Objective-C on Linux.
This is what we want to end up with:
let ma = ObjC.NSMutableArray() ma.addObject("Hello") .addObject("World") print("Array:", ma.description())
For demonstration purposes only: This is just a demo showing what you can do with @dynamicCallable, nothing more! (we also cheat a few times and silently rely on builtin bridging.)
Install a Swift 5 Snapshot
For this to work, you need to have Swift 5 installed. As of today Swift 5 hasn't been released yet, but Apple is providing prebuilt Swift 5 development toolchains over at: swift.org, and you can also use the Xcode 10.2beta. Just install the toolchain, and select it in the Xcode "Toolchains" menu.
If you are living on the commandline, make sure to call
export TOOLCHAINS=swift to get access to the selected version.
To try out the samples below, you can either open a Swift 5 Playground or create a command line tool project project in Xcode.
Brought to you by ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.