Maintaining a Swift and Objective-C Hybrid Codebase

6 minute read Swift is gaining popularity among iOS developers, which is of no surprise. It's strictly typed, which means you can prove the correctness of your program at compile time, given that your typesystem describes the domain well. It's a modern language offering syntax constructs encouraging developers to write better architecture using fewer lines of code, making it expressive. It's more fun to work with, and all the new Cocoa projects are being written in Swift. At Shopify, we want to adopt Swift where it makes sense, while understanding that many existing projects have an extensive codebase (some of them written years ago) in Objective-C (OBJC) that are still actively supported. It's tempting to write new code in Swift, but we can't migrate all the existing OBJC codebase quickly. And sometimes it just isn't worth the effort.

Adrianna Chang
7 min readintermediate
--
View Original

Overview

The article discusses the challenges and strategies for maintaining a hybrid codebase that combines Swift and Objective-C at Shopify. It emphasizes the importance of interoperability between the two languages while leveraging Swift's modern features and type safety.

What You'll Learn

1

How to maintain a hybrid codebase using Swift and Objective-C

2

Why nullability annotations are critical for Swift interoperability with Objective-C

3

How to use the @objc attribute for bridging Swift and Objective-C

4

When to use extensions in Swift to enhance Objective-C classes

Prerequisites & Requirements

  • Understanding of Swift and Objective-C programming languages
  • Familiarity with iOS development and Cocoa frameworks(optional)

Key Questions Answered

How can Swift and Objective-C code coexist in a hybrid codebase?
A hybrid codebase is achievable by carefully managing interoperability between Swift and Objective-C. Developers can call Objective-C components from Swift and vice versa, but must be mindful of certain language features and type safety to prevent runtime crashes.
What role do nullability annotations play in Swift and Objective-C integration?
Nullability annotations, such as nullable and nonnull, are essential for ensuring that Swift correctly interprets Objective-C pointers. Incorrect annotations can lead to runtime crashes, as Swift relies on these annotations to enforce type safety.
What are the limitations of using Swift features in Objective-C?
Modern Swift features like enums and structs are not visible in the Objective-C runtime. However, developers can create Objective-C-compatible wrappers around Swift types to extend their usability in Objective-C code.
How does the @objc attribute enhance interoperability between Swift and Objective-C?
The @objc attribute allows Swift classes and methods to be accessible in the Objective-C runtime. This attribute is crucial for ensuring that Swift code can interact with existing Objective-C codebases effectively.

Technologies & Tools

Some links below are affiliate links. We may earn a commission if you make a purchase.

Programming Language
Swift
Used for writing new code in the hybrid codebase.
Programming Language
Objective-c
Existing codebase that is actively supported.

Key Actionable Insights

1
Implement nullability annotations in your Objective-C APIs to ensure safe interoperability with Swift.
This practice is vital as it prevents runtime crashes due to incorrect assumptions about pointer nullability, enhancing the stability of your hybrid codebase.
2
Utilize the @objc attribute strategically to expose Swift classes and methods to Objective-C.
This allows for seamless integration of new Swift features into existing Objective-C projects, promoting code reuse and reducing the need for extensive rewrites.
3
Consider creating NSObject subclasses to wrap Swift-only types for use in Objective-C.
This approach enables you to leverage Swift's modern features while maintaining compatibility with legacy Objective-C code, facilitating a smoother transition.

Common Pitfalls

1
Failing to annotate Objective-C APIs with correct nullability can lead to runtime crashes in Swift.
This occurs because Swift relies on these annotations for type safety. Without them, developers may encounter unexpected nil values when accessing Objective-C pointers.
2
Not making Swift symbols public can prevent them from being accessed in Objective-C.
This limitation can hinder the integration of Swift code into Objective-C projects, making it crucial to manage visibility appropriately.

Related Concepts

Interoperability Between Swift And Objective-c
Type Safety In Programming
Bridging Patterns In Hybrid Applications