Search

Migrate Objective-C Code To Swift


It’s been quite sometime since Swift has come in the market. As it provides many advantages over Objective-C, developers are moving their legacy Objective-C code to Swift. When I did it first, I thought it was better to re-write the app from scratch again in Swift. But it is possible to migrate the same legacy code to Swift with little or less hassle. You can actually improve the architecture, logic and performance by replacing pieces of Objective-C to Swift. The following steps will make the code better and also you can migrate the code from Objective-C to Swift very swiftly and easily. Let’s begin


Clean Up Your Code

The first thing is to make sure that the Objective-C code and Swift code have best or most favourable compatibility. You can do this by modernizing and cleaning up the existing Objective-C code. For example, if you find out that there are parts of the code which don’t have nullability annotations, now is the time to add them. Your code must follow modern coding practices. This is to make sure that Objective-C interacts with Swift without any problem and the communication is effective.


Migrate The Code

The best way to do this is to migrate to Swift one class at a time. It is best to start with the classes that don’t have any subclasses. The reason behind this is that you can’t subclass Swift classes in Objective-C. Replace the .m and .h files for that class with a single .swift file. Whatever you have in your implementation and interface files will directly go into this single Swift file. There’s no need to create a header file since Xcode automatically generates a header in case you need to referent it.

  1. 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 that your Objective-C class. Class prefixes are optional in Swift.

  2. Import all relevant system frameworks.

  3. Complete the Objective-C bridging header if you need to access Objective-C code from the same app target in your Swift file.

  4. 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 @objc<name>, where name is the name that your Objective-C code uses to reference the Swift class.


As You Migrate

  1. You can set up your Swift class to integrate Objective-C behaviour by subclassing Objective-C classes, adopting Objective-C protocols and more.

  2. As you work with Objective-C APIs, you will need to know how Swift translates certain Objective-C language features. For these features you can see THIS.

  3. Use the @objc<name> attribute to provide Objective-C names for properties and methods when necessary.

  4. You can denote instance methods (-) with “func” and class methods (+) with “class func” respectively.

  5. Declare simple macros as global constants and translate complex macros into functions.


When Finished

  1. Update import statements in your Objective-C code to refer to the new Swift code.

  2. Remove the original Objective-C implementation file from the target by deselecting the target membership checkbox. Don’t yet delete the .m and .h files immediately. You might still need them to troubleshoot.

  3. Update your code to use the Swift class name instead of Objective-C name if you gave the Swift class a different name.


Troubleshooting Tips and Reminders

Migration experiences can differ based on your existing code. Here are some general steps and tools to help you troubleshoot the process.

  1. A Swift class cannot be subclassed in Objective-C. The class you migrate can’t have any Objective-C subclass.

  2. When you migrate a class to Swift, the corresponding .m or the implementation file must be removed from the target as it will send you a duplicate symbol error when you build.

  3. You’ll need to make your Swift file a descendant of the Objective-C class to make it available in Objective-C.

  4. Command-c a Swift class name to see its generated header.

  5. Option-click a symbol to see implicit information about it, like its type, attributes and documentation.

97 views0 comments

Recent Posts

See All