# 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-21620.md>)

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

Author: Miguel Angel Quiñones

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

Content type: tutorial

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>), [Code](<https://devfeed.tech/topics/code.md>), [Code generation](<https://devfeed.tech/topics/code-generation.md>), [Programming](<https://devfeed.tech/topics/programming.md>)

Tags: [bridging](<https://devfeed.tech/tags/bridging.md>), [code-generation](<https://devfeed.tech/tags/code-generation.md>), [examples](<https://devfeed.tech/tags/examples.md>), [generate](<https://devfeed.tech/tags/generate.md>), [objective-c](<https://devfeed.tech/tags/objective-c.md>), [programming](<https://devfeed.tech/tags/programming.md>), [swift](<https://devfeed.tech/tags/swift.md>)

## AI overview

This tutorial explains how to automate bridging Swift structs and enums to Objective-C using Sourcery templates and metadata from production code. It introduces a protocol-based opt-in approach, describes separate generation strategies for structs and enums, and notes the use of Stencil for template development.

## 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: 1 protocol AutoObjCBridgeable {} For example, take Benjamin's example code: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 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.checkout