Swift and Objective-C are two programming languages used to create applications for the iOS/OS X ecosystem. However, lately, Apple is pressing for the transit of development from Objective-C to Swift. The number of requests like “how to migrate an app from Objective-C to Swift?” is growing by the day. It makes sense if you want to improve your skills and strive to follow the top trends in the development of mobile software. After all, migrating a ready project is a wonderful way to study this relatively new language. In addition, such an exercise allows making the application code more concise and efficient, providing the possibility of implementing some of its functions by using the more advanced tools inherent to Swift.

Read also: Objective C vs Swift Comparison

In the article, we will provide a systematic guide describing how to move from Objective C to Swift for already created projects.

Nine Consecutive Stages that Allow to Migrate From Objective C to Swift

1. Prepare the Code

Before starting to migrate from Objective C to Swift, one would need to check the compatibility of their software with Swift 3 (the latest release of this programming language) and preparedness to future release of Swift 4 (conversion with the help of Xcode Tools). Indeed, in view of the more than 20 years of Objective-C existence, developers have repeatedly changed their approach to creating high-performance, secure and readable code. Therefore, before translating an app, its code in Objective-C should be maximally laconic, should not use unnecessary memory resources and all the variables should be typed strictly (this is especially important for applications created more than two years ago). For instance, you should employ ‘instancetype’ instead of ‘id’, properties instead of instance variables, ‘NS_ENUM’ macro for enumerations, ‘NS_DESIGNATED_INITIALIZER’ macro for object initialization, and ARC for automatic memory management. Alternatively, if you want to minimize the possibility of bugs after the manual code refactoring, use the Xcode converter (nevertheless, after running the code through this converter, be sure to carefully check and confirm all the changes made).

convert objectiveC to swift

If you employ the Git service, spawn a new branch in its storage for the resulting code.

2. Convert Objects

Now let us proceed to the translation process itself. A correct approach to its implementation is the file-by-file and class-by-class translation. At the same time, it is necessary to realize that, most likely, you will not be able to build the application completely in Swift and it will retain some Objective-C modules. This is explained by the fact that classes declared in Objective-C cannot subclass from Swift objects. Therefore, specific classes with their derivatives would have to stay intact. Remember, however, that in order for a new class declared in Swift to be accessible in Objective-C, it must be declared as a descendant of an existing Objective-C class. In turn, for converting to Swift, those classes are viable that do not contain any subclasses. To translate the code describing them, you just need to replace the files with the .m extension (containing the code) and the .h (header file) with a single .swift file, which will include both interface descriptions and functional code. To do this, you need to choose the menu command “File -> New -> File -> iOS/watchOS/tvOS/macOS -> Source -> Swift File” and check whether it is added to the assembly. It is not necessary to create header files – the Xcode automatically generates them.

3. Translate Import Sections

Next, you need to delete the lines with the #import statements that are present in the files “tied” to the remote header files. Instead, declare the import of the hidden title link.

4. Create Definitions

Now, some additional manipulations with the newly created .swift files must be done. In them, you must declare classes and set definitions/stubs with respect to all elements declared in the remote headers with the ‘public’ access modifier. In addition, you will need to specify the appropriate #import statements in the bridge header file if you need to connect any files that have the original Objective-C code in them.

5. Validate the Project Structure

Assemble and launch the newly created .swift files one by one, verifying the correctness of their structure. The compiler may discover some irregularities in definitions, imports and class specifications. Correct all the discovered errors. Repeat the same with all the .m and .h files transferred from the Objective project. Now we have the file structure for the converted project ready. Of course, .swift files do not contain any actual code yet.

6. Translate the Code

Next would be the preliminary line-by-line transcription of the Obj-C code into Swift.

7. Migrate the Delegate Class

After checking the files, finally, proceed with the migration of the delegate class. If you were lucky and there was no need to add any of the Obj-C files to the resulting Swift project, you can completely delete the ‘main.m’ file, inserting instead ‘@UIApplicationMain’ attribute into the app delegate class declaration, as well as the pre-compiled .pch header file.

8. Clean-up the Code

At the final stage of transitioning from Objective C to Swift, try to refine the Swift code. Since all your previous activities were aimed at simply creating a “working” version of the app, you will have to rewrite it somewhat, guided by the best practices of this programming language. Most likely, you will find a number of code blocks, which can be made more concise and less resource consuming. Try at the same time to use the native Swift types as much as possible. For example, instead of the types specific to Objective-C, such as ‘NSString’, ‘NSMutableString’, ‘NSArray’, and ‘NSMutableArray’, as well as ‘NSDictionary’ and ‘NSMutableDictionary’, you can use the types specific to Swift: ‘String’, ‘Array’, and ‘Dictionary’. Another point to check for are constant/variable declarations. In Swift, immutable constants are declared using keyword ‘let’, mutable variables – ‘var’.

migration objectiveC

9. Test the Translated Project

Despite your Obj-C application already being tested extensively, after its conversion to Swift, it should be checked all over again to find faults brought by the human factor and differences in Objective and Swift programming conceptions. This is actually a great opportunity to test one of the winning features of Swift – protocol default implementation.

How to Convert Objective C Project to Swift: Final Thoughts

And finally – the main recommendation.

Do not seek to convert Objective C project to Swift completely. It is likely that in addition to the classes that are inherited from Objective-C, there will be a number of constructs in the code that are more successfully expressed in its original form. Also, you will have to leave certain elements from the Cocoa API that are not available in Swift untouched.

Let us sum up. Translating an application from Objective-C to Swift is a straightforward staged procedure. And, if you use the tutorial given in this article, you will be guaranteed to raise your developer skills for the iOS platform to a new level.

Read also: Reactive Programming with RxSwift

How to Convert Your Objective C Project to Swift
5 based on 1 user ratings