Ready to bite the bullet and migrate from Objective-C to Swift

I've been publishing iOS apps since 2009, a couple of which have grown quite large in that time (over 200 classes in the largest), all in the original Objective-C language. I've kept an eye on Swift and started some new projects in Swift, but the task of rewriting my existing apps has been too big to make a priority. Then this year, the economic slowdown from COVID has given me more time, and I've decided to tackle it this fall. At the same time, I've been noticing a stronger bias toward Swift in Apple's documentation and code samples. For example, new framework documentation only includes Swift code samples, while older documentation includes both. This confirms that the time is right to make this investment.

I've found some good tips for managing the migration, including this article:

https://developer.apple.com/documentation/swift/migrating_your_objective-c_code_to_swift

My question is more general: if you migrated a large project, I'd be curious to hear something about your overall experience. What made you decide to go for it, was it easier or harder than you expected, are you happy with the results?

BTW, I ported some of my apps to Android back in 2013, so I have some sense of the scale of the project. I'm expecting this to be easier than that since most of the classes and frameworks are the same, but with slightly different names and a very different syntax. Simply retyping thousands of lines of code with a new syntax can be exhausting, though.

Replies

I never had my own projects to migrate, but for learning, I had some sample projects (notably one for CollectionView) that I wanted to write in Swift from an objC code.

I found it pretty hard notably when it involved unsafe pointers (hopefully I got some good help from OOPer, thanks to him).
And I found that self proclaimed code conversion tools did not make it.

And finally, Apple doc was nearly no help.
I went ahead with this project and am at the beta testing stage now. So I can partially answer my own question.

First, it was harder than I expected. I previously converted the Android version of the same app from Java to Kotlin, so I have that experience for comparison. The Objective-C to Swift conversion took twice as many hours as the Java to Kotlin conversion. The main reasons for this were:
  1. Obj-C/Swift interoperability is not nearly as smooth as Java/Kotlin interoperability. I was spending so much time trying to get Obj-C and Swift code to work together that I finally gave up and didn't even try to build again until all the files were converted. I wrote more about this on StackOverflow, but this Apple site won't let me link to that (?!?!).

  2. Xcode does more enforcing of Swift style, like renaming my functions, but that didn't work consistently and required a lot of manual cleanup.

  3. I think Swift is simply more different from Obj-C than Kotlin is from Java, in syntax and functionality. Dictionaries aren't objects, properties aren't atomic, similar types like Float and CGFloat aren't interchangeable ... this all required rework and sometimes wasn't evident until testing revealed new bugs.

I used Swiftify's Advanced Project Converter (but just converted a few files at a time as they recommend), which is similar to the Java to Kotlin converter built into Android Studio. Both of these tools required some manual cleanup after converting. More annoyingly, both imposed some coding style and formatting changes that I didn't agree with. For example, they both stripped out parentheses from complex conditional expressions that weren't strictly necessary but that made the expressions more readable. They also both stripped out the explicit use of self (or this) when referring to class variables, which I consistently do in all my code. I spent around a quarter of the project time stubbornly putting my code back to the way I wrote it. If you don't mind potentially adopting a new style guide, that will be a big chunk of work you won't have to do. Even so, I think the project would have been impractical without these tools.

Xcode really bogged down after converting a large file and trying to show all the new errors that generated. Often the compiler just gave up and didn't show me anything. Sometimes the interface was so slow I had to copy code into BBEdit, work on it there for a while, then copy it back into Xcode. I have a three-year old MacBook Pro and could have benefitted from a faster computer. (I was ready to buy one a few weeks into this, but I'm holding out for a 16" with Apple Silicon.)

It was tempting to do some refactoring at the same time as the conversion, but this usually created more messes. I would recommend doing any refactoring before or after the conversion, but not combining it with the conversion.

Now that I've gotten through it, I'm already used to Swift, and Objective-C feels awkward. I'm dreading maintaining some of my other apps that are still in Objective-C. I think moving forward with Swift will be more efficient and more enjoyable. But it was an expensive investment, and looking at it purely in terms of hours saved, will take a few years to pay off.
Interesting feedback.
I appreciate Swift strong type enforcement, even though I often complain when having to cast to CGFloat from a Float.

I agree with you, this forum is really frustrating to say the least, with so limited capabilities.
Can't you link to a SO thread ? That would be a new limitation (but I notice since a few days that my posts with links are systematically removed. I'll try to post a dummy SO reference, just to see, in another reply.
Dummy reply, just to test posting a reference to SO
https://stackoverflow.com/questions/66328696/scroll-collectionview-all-row-at-same-direction
I'll try the link again:

https://stackoverflow.com/questions/52942575/how-to-convert-giant-project-objective-c-to-swift/65927556#65927556

Edit: Huh, it worked that time!