GitXplorerGitXplorer
h

SwiftObjCBridge

public
45 stars
2 forks
0 issues

Commits

List of commits on branch master.
Unverified
e19641e42f6f09b40d41be116100011a08ab2852

Fix tests

hhelje5 committed 6 years ago
Unverified
f1ffdb63e39e6182068c32afeca0062d6dbf3004

First drop

hhelje5 committed 6 years ago
Verified
41fe94db105fd0ce4b8516c97d2a8c6af8ffe04f

Initial commit

hhelje5 committed 6 years ago

README

The README file for this repository.

Swift - Objective-C Bridge

Swift5 macOS

Part of this blog post: @dynamicCallable Part 2: Swift/ObjC Bridge.

In December we demonstrated how to use the new Swift 5 Dynamic Callable feature to run Unix commands as Swift functions, like shell.ls(). 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 bridging runtime.
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.

Links

Who

Brought to you by ZeeZide. We like feedback, GitHub stars, cool contract work, presumably any form of praise you can think of.