Introducing Binding Tools for Swift

If you follow my blog, you’ve probably noticed two things: it’s been quiet here as of late and I haven’t spoken directly about what I’ve been working on since I joined Xamarin. Let’s change both of those things.

Today a vast majority of the APIs and libraries powering Apple’s ecosystem (iOS,macOS, tvOS, watchOS) come in the form of Objective-C libraries. Xamarin’s binding project support can expose these APIs in C# for use the same as any other managed library. The same infrastructure powers the platform assemblies, like Xamarin.iOS.dll.

A few years ago Apple introduced a new programming language called Swift, which has been under active development with a number of breaking changes over the years due to the evolution of the language. Swift by default has a very different runtime design than other languages. This makes calling Swift code exceptionally difficult from C# in most cases.

Solving this problem required writing a tool significantly more complex than our existing binding infrastructure. One advantage of Swift libraries, however, is that they contain enough information to automatically create a reasonable API mapping to C#. This means we can improve upon the experience of binding Objective-C by skipping the need for a user to define the interface in C# by hand.

After years of development and 1200 unit tests and tests on 3rd party libraries, we’re happy to announce that we’re open sourcing our work:
https://github.com/xamarin/binding-tools-for-swift/

Current Status

Binding Tools for Swift currently works on most of the common cases for in Swift 5.0 code, but there are limitations that we are actively working on:

  • Project and MSBuild support to make it easier to use
  • Support for Protocols with Associated Types
  • Support for Swift 5.1
  • Support for non-escaping closures
  • Support for closures in bound generic types
  • Improving documentation and samples

There is also work that needs to be done to fully integrate BTfS into Visual Studio for Mac and bring it into a public preview.

Get Involved!

It’s been a lot of work and a lot of typing. I’m very happy with where the code is today and am looking forward to getting my first community pull request. Get involved! I’ve written a lot of documentation on how the tool works and you can read the quickstart guide here.

I’m proud of the work that I’ve done and I’m very happy to bring Swift interoperability into the .NET ecosystem. Come help me out!

5 thoughts on “Introducing Binding Tools for Swift”

  1. why?
    swift is already crossplatform (mac, linux, android, windows)

    just focus on that, idk why people always want to add overhead on top of overhead, just to use language a designed for servers

    swift is miles ahead of everything

    1. I don’t reply to a lot of comments, but I will point out that the main use case for BtfS is for people who are writing apps in C# for iOS/macOS devices and want to access libraries written in Swift. Moving forward, Apple will shift to producing only libraries in Swift instead of ObjC. Proving tools to access them is a logical win.
      If you like Swift on its own – that’s great. Use it! Not everyone is in that camp, which is why there are languages like C#, F#, Haskell, ObjC, C, Python, Ruby, Javascript, Scheme etc. Turing equivalence says that none is more capable than any other and each brings something unique to the table.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.