I’ve released version 3.0 of Priorities App a few weeks ago and added a new todo feature with this update to the app. This is what users and customers see when they work with Priorities App and use it in their daily business. But that’s only the foreground. Behind the scenes, Priorities App has completely changed and was completely refactored with this new version 3.0. In concrete, the complete code base has changed. I’ve rewritten the app completely in Swift and migrated all Objective-C classes to Apples new programming language. And in this article I want to give you an insight of the experiences I ‘ve made while I migrated a complete iOS project.
First I have to say that Priorities App is the smallest app project I’m working on (both Writing App as well as Writers App – my two other iOS projects – have a bigger code base). This was the reason I decided to try the migration with Priorities App and not with any of the two other projects. But before I go into the deeper details about how I did the migration, I guess I need to answer another question you may have in mind: Why (the hell!!!) have you done this painful peace of work?
Why the hell did you migrate to Swift?
From October to December 2014 I’ve written a textbook about Apples new programming language Swift which will be released in March 2015. While writing this textbook I’ve learned all the details as well as the great improvements Swift offers me and I really love the way Swift code looks and how Swift works. Furthermore I needed some test projects to work on (not only the Playgrounds that are offered by the IDE Xcode to test Swift code and to play around with it) and so I’ve started a completely new project (on which I’m working since today), but that wasn’t enough. I also wanted to write about the migration features and possibilities you have to switch from Swift to Objective-C or how to mix your code. So, what did I do? I picked up my smallest iOS app project – better known as Priorities App – and started the migration; class by class by class.
When I started migrating Priorities App I knew one thing for sure: I wanted to migrate the whole project completely to Swift; no Objective-C class should be left behind. And so I did it the hardcore way: I have rewritten every little class from the project by creating a new appropriate Swift class and rewriting the code in a common way. Furthermorde I didn’t use any bridging header to get the app working with a mixed code base of Swift and Objective-C code. I feared that such a mix would stop me working on the migration as soon as it gets a little bit complex or tricky. And I definitely didn’t want to work with a mixed Objective-C and Swift code base, this was not my goal.
So, yeah, I did it as described: I grapped a class, created a new appropriate Swift class and rewrote the code in a common way that was fine for Swift. Sounds a little bit crazy? Well, yes, it was a little bit crazy. 🙂 But as you can see: The result works as expected! 😀
Why migration worked so easily for Priorities App
But as you may think I wouldn’t recommend this way of migration to anyone else. There are two reasons why this workflow worked without any problems for Priorities App:
First, as I mentionend above, Priorities App is a small project with about 30 classes. The appropriate model and view classes doesn’t have many code lines (I guess the maximum of code for one of these classes is about 100) and the controller don’t have more than about 400 lines of code.
Second – and this is one of the most important points that made migrating Priorities App from Objective-C to Swift so easy – I had a very clean and structured code base for the app; already with Objective-C. All classes were structured by the MVC model (Model-View-Controller) and I put only the really nececssary code in an appropriate class; if necessary, I created several helper classes to optimize the structure and functionality of my code.
This clean code base made it possible to migrate my classes easily from Objective-C to Swift. If my code base would have been unstructured, unclean or horrible in any other way, I guess the migration would have been a pain in the ass (sorry for my wording, but I think this is the definitely best description 🙂 ).
My recommendations for migrating from Objective-C to Swift
So, what are the results you can take from my experiences? I have the following suggestions for everyone who wants to migrate an Objective-C project to Swift:
- Clean and refactor your code! Check your classes and your class structure and may refactor some of your Objective-C code before starting to migrate everything to Swift.
- Migrate class by class, not everything at once! If you try to refactor several classes at once (because you may have dependencies between them) you can end up in an infinite loop and a code chaos which is horrible to handle.
- Use bridging header while you migrate and test your code! Yeah, funny, exactly what I haven’t done is what I tell you as advice. But the only reason I dind’t do this was that Priorities App is a small project. I did the migration within one working day (including testing) and of course this is not the case for most app projects out there. So please, use bridging header, migrate your code and your classes step by step and test on a regular basis. I will do that myself for any other projects I’ll migrate to Swift in the future.
I hope you’ve enjoyed my little developer insights (you can be sure that this wasn’t my last article about my developer life 😉 ). As you can see I’m already a big fan of Apples new programming language Swift and my goal is to completely migrate all my projects to Swift so that I’ll primary work with this programming language in the future.
I would really love to hear from other developers what you think about Swift and about the experiences you have made with this programming language or with migration from Objective-C. Just leave a comment with your thoughts, I would be glad to hear about the things you have to say about this topic.