Learn the recommended steps to migrate your code.
- Swift Standard Library
You can improve the architecture, logic, and performance of one of your Objective-C apps by replacing pieces of it in Swift. Interoperability makes it possible to integrate features migrated to Swift into Objective-C code with no hassle. You don't need to rewrite your entire app in Swift at once.
Clean Up Your Code
Make sure that your Objective-C code and Swift code have optimal compatibility by tidying up and modernizing your existing Objective-C codebase. For example, if there are parts of your codebase to which you haven't added nullability annotations, now's the time to add them. Make sure your code follows modern coding practices so that it interacts more effectively with Swift.
Migrate Your Code
The most effective approach for migrating code to Swift is on a per-file basis—that is, one class at a time. Because you can’t subclass Swift classes in Objective-C, it’s best to choose a class in your app that doesn’t have any subclasses. You’ll replace the
.h files for that class with a single
.swift file. Everything from your implementation and interface goes directly into this single Swift file. You won’t create a header file; Xcode generates a header automatically in case you need to reference it.
Create a Swift class for your corresponding Objective-C .m and .h files by choosing File > New > File > (iOS, watchOS, tvOS, or macOS) > Source > Swift File. You can use the same or a different name than your Objective-C class. Class prefixes are optional in Swift.
Import relevant system frameworks.
Fill out an Objective-C bridging header if you need to access Objective-C code from the same app target in your Swift file.
To make your Swift class accessible and usable back in Objective-C, make it a descendant of an Objective-C class. To specify a particular name for the class to use in Objective-C, mark it with
), where name is the name that your Objective-C code uses to reference the Swift class.
As You Work
You can set up your Swift class to integrate Objective-C behavior by subclassing Objective-C classes, adopting Objective-C protocols, and more.
As you work with Objective-C APIs, you’ll need to know how Swift translates certain Objective-C language features. For more information, see Objective-C and C Code Customization.
)attribute to provide Objective-C names for properties and methods when necessary.
Denote instance (
-) and class (
+) methods with
class func, respectively.
Declare simple macros as global constants, and translate complex macros into functions.
After You Finish
Update import statements in your Objective-C code (to
#import "Product) to refer to your new Swift code.
Module Name-Swift .h"
Remove the original Objective-C
.mfile from the target by deselecting the target membership checkbox. Don’t delete the
.hfiles immediately; use them to troubleshoot.
Update your code to use the Swift class name instead of the Objective-C name if you gave the Swift class a different name.
Troubleshooting Tips and Reminders
Migration experiences differ depending on your existing codebase, but here are some general steps and tools to help you troubleshoot the process:
Remember that you can't subclass a Swift class in Objective-C. Therefore, the class you migrate can't have any Objective-C subclasses.
Once you migrate a class to Swift, you must remove the corresponding
.mfile from the target before building to avoid a duplicate symbol error.
To make a Swift class available in Objective-C, make it a descendant of an Objective-C class.
Command-click a Swift class name to see its generated header.
Option-click a symbol to see implicit information about it, like its type, attributes, and documentation comments.