SoFunction
Updated on 2025-04-11

How to port Objective-c code to Swift code Introduction to transfer Objective-c code to Swift

The migration work just provides an opportunity to revisit existing Objective-C applications, and can also use Swift code to better optimize the application's architecture, logic and performance. To put it bluntly, you will use the mix and match you learned previously and the interoperability between the two languages ​​to perform incremental migration. The Mix-and-match feature makes it simple to choose which features and functions to implement with Swift and which ones are still implemented with Objective-C. The interoperability of Swift and Objective-C in turn makes it less difficult to integrate these features into Objective-C. These tools allow you to open up Swift's extensions and integrate them into existing Objective-C projects without having to rewrite the entire project immediately using Swift.

Prepare for migration for your Objective-C code

Before you start migrating your code, make sure that your Objective-C and Swift code have the best compatibility. This means organizing and using the modernization of Objective-C to optimize your existing projects. To interact with Swift more seamlessly, your existing code needs to follow modern coding practices. Here is a short list of adaptation exercises, seeAdopting Mordern Objective-C

Migration process

The most efficient way to migrate code is to be based on file-by-file approach, that is, to complete one class at a time. Since you cannot inherit Swift classes in Objective-C, it is best to choose one that has no subclasses (Translator: From the perspective of class inheritance, you should start with the leaf nodes of the class tree and migrate from the bottom up). You can use a single .swift file instead of the corresponding .m and .h files. All your implementation code and interfaces will be placed directly into a single Swift file. You no longer have to create header files; Xcode will automatically generate header files when you need to reference them. (Translator: Of course, this is actually done by the internal xcode mechanism and is transparent to developers)

Preparation

•In Xcode:File>New>File>(iOS or OS X) > Other > Swift Create a Swift class for the corresponding and .h files.
•Import relevant system frameworks.
•If you want to access Objective-C code in a Swift file, you can fill in an Objective-C bridge header. For specific operation steps, please seeImporting Code from Within the Same App Target
•To enable your Swift class to be accessible and used in Objective-C, you can inherit the Objective-C class, or tag the @objc attribute. Assign a special name to the class to use in Objective-C, mark @objc(#name#), <#name#> is the Swift class name referenced in Objective-C. For more information, please seeSwift Type Compatibility

Get started

•You can integrate Objective-C behavior by inheriting the Objective-C class, adapting the Objective-C protocol, or more. For more information, please seeWriting Swift Classes with Objective-C Behavior
•When you use Objective-C APIs, you need to know how Swift translates certain Objective-C features. For more information, please seeInteracting with Objective-C APIs
•When writing code using the Cocoa framework in Swift, remembering that certain types are bridged means that you can use certain Swift types instead of the Objective-C type. For more information, please seeWorking with Cocoa Data Types
•When you use the Cocoa design pattern in Swift, please seeAdopting Cocoa Design PatternsGet more information on conversion of general design patterns.
•For those who intend to convert projects from Objective-C to Swfit, please seePropeties
• When necessary, provide the Objective-C name for the Swift property or method through the @objc(<#name#>) property, just like this:

Copy the codeThe code is as follows:

var enabled: Bool {
    @objc(isEnabled) get {
        /* ... */
    }
}

• Use func and class func to represent the instance(-) and class(+) methods respectively.
•Declare simple macros as constants and convert complex macros into functions.

Completed

•Update the import statement in your Objective-C code to #import "module name-", inImporting Code from Within the Same App It was mentioned in Target.
• Remove the check box in the member selection box of Target to remove the original file. Do not delete the .m and .h files immediately for the problem to be solved.
•If you give the Swift class a different name, use the Swift class name instead of the Objective-C name.

Problem resolution tips

The migration experience is different for different projects. In any case, there are some common steps and tools that can help you solve problems encountered during code migration:

•Remember: You cannot inherit Swift classes in Objective-C. Therefore, the classes you migrate cannot have any Objective-C subclasses in your application.
•When you migrate a class to Swift, you must remove the relevant .m file from the target to avoid compilation errors such as duplicate symbols when compiling.
•In order to be accessible and used in Objective-C, the Swift class must be a subclass of the Objective-C class, or be marked as @objc.
•When you use Swift code in Objective-C, remember that Objective-C cannot understand some of the features of Swift, please seeUsing Swift from Objective-C
•You can view the header file it generates by Common + clicking on a Swift class name.
• You can view more detailed information through Option + click on a symbol, such as its type, properties, and document comments.