# Automatic bridging from Swift to Objective-C using Sourcery

DevFeed: [Automatic bridging from Swift to Objective-C using Sourcery](<https://devfeed.tech/articles/automatic-bridging-from-swift-to-objective-c-using-sourcery-97440.md>)

Original publisher: [Read original article](<https://miqu.me/blog/2017/05/21/automatic-bridging-from-swift-to-objective-c-using-sourcery/>)

Published: 2017-05-21T21:30:00Z

Content type: article

Language: en

Sources: [Miguel Quinones](<https://devfeed.tech/sources/miguel-quinones.md>)

Topics: [Swift](<https://devfeed.tech/topics/swift.md>), [Objective-C](<https://devfeed.tech/topics/objective-c.md>), [Boilerplate](<https://devfeed.tech/topics/boilerplate.md>)

Tags: [automated](<https://devfeed.tech/tags/automated.md>), [bridging](<https://devfeed.tech/tags/bridging.md>), [class](<https://devfeed.tech/tags/class.md>), [code](<https://devfeed.tech/tags/code.md>), [enum](<https://devfeed.tech/tags/enum.md>), [enums](<https://devfeed.tech/tags/enums.md>), [init](<https://devfeed.tech/tags/init.md>), [meta-programming](<https://devfeed.tech/tags/meta-programming.md>), [metadata](<https://devfeed.tech/tags/metadata.md>), [metaprogramming](<https://devfeed.tech/tags/metaprogramming.md>), [objective-c](<https://devfeed.tech/tags/objective-c.md>), [properties](<https://devfeed.tech/tags/properties.md>), [protocol](<https://devfeed.tech/tags/protocol.md>), [protocols](<https://devfeed.tech/tags/protocols.md>), [string](<https://devfeed.tech/tags/string.md>), [struct](<https://devfeed.tech/tags/struct.md>), [structs](<https://devfeed.tech/tags/structs.md>), [swift](<https://devfeed.tech/tags/swift.md>), [template](<https://devfeed.tech/tags/template.md>), [templates](<https://devfeed.tech/tags/templates.md>), [tools](<https://devfeed.tech/tags/tools.md>)

## AI overview

Sourcery can generate Objective-C-accessible wrapper classes for opted-in Swift structs and enums. Types adopt the marker protocol `AutoObjCBridgeable`; a Stencil template uses Sourcery metadata to create `NSObject` wrappers that hold the native value and expose properties. Struct wrappers forward native properties, with setters for mutable values. Enum bridging needs a wrapper for the enum plus separate classes for its cases, allowing associated values to be represented. The example is deliberately incomplete: it does not handle protocols, private properties, initializer defaults, forwarded methods, or structs used as enum associated values. The author presents generated bridges as a temporary aid during migration and recommends keeping Objective-C interaction limited.

## Source excerpt

I recently read a great post by Benjamin Encz about bridging Swift to Objective-C by creating bridging types that can be exposed to the older language. I wondered if his idea can be improved and automated using meta-programming with Sourcery by Krzystof Zablocki. The answer is a very impressive ObjC-like uppercase YES. First of all, you should read Benjamin's post. He proposes to use a bridging class that can be instantiated from Objective-C and Swift because it inherits from NSObject. The bridging class has properties to get and set the properties of the bridged type. In this post I'm just going to give an overview of the template and highlight the simpler parts. If you want to jump directly in the code, here's the project, and the template. The example template bridges struct and enum, but doesn't handle custom protocols to keep it as simple as possible. About Sourcery Sourcery is a tool to generate code, using your written templates, using metadata from your production code. In other words, it's a tool to do metaprogramming in Swift. If you are not familiar with Sourcery I encourage you to go read the documentation and at least understand the simple examples like generating Equatable implementations for your types. Getting familiar with Stencil, one of the supported template languages is also fundamental. Building the template We'll need to generate a new class for every struct and enum that we bridge. In order to opt-in to the feature, we use phantom types in order to mark swift types (our custom structs or enums) that can be bridged to Objective-C. Our template will use this protocol to which types to introspect: protocol AutoObjCBridgeable {} For example, take Benjamin's example code: enum CheckoutOption { case creditCard(given: Int) case paypal(String) } struct ShoppingCart { var items: [String] var checkoutOption: CheckoutOption? init(items: [String], checkoutOption: CheckoutOption? = nil) { self.items = items self.checkoutOption = checkoutOption } } We just