Aleem
Aleem

Reputation: 3291

How to convert Giant project Objective C to Swift

I've running project and its really gigantic, it contain almost 1000 files and 4 Custom (own built) framework and almost 10 others added via Pods. I've gone through Migrating Your Objective-C Code to Swift and also Migrate with Swiftify.

I started to converting each file one by one as Apple suggest but first Conversion isn't successfully done by Swiftify and also dependency issues.

So at this position its looks like that I start walking in Sahara Desert, where I can't see any end point.

So I need some suggestion how to convert to Swift this kinda huge scale project?

Upvotes: 12

Views: 16506

Answers (4)

arlomedia
arlomedia

Reputation: 9071

Here's a different perspective from the other answers. I have a project of similar size to the original poster (250 classes, 7 MB of source code). I don't want the mental load of maintaining a hybrid of two different programming languages long-term. And after converting about 30 classes, I found myself spending most of my time tweaking code for interoperability between Objective-C and Swift. Issues included:

  • Some types like Array and NSMutableArray aren't automatically interchangable, so I had to insert a lot of extra type casting. Even Objc-C int and Swift Int require casting.

  • Other types like enums have limited support in Obj-C -- for example, Swift enums can't be used as a function parameter type -- so I was limited in what new Swift features I could use. I found myself doing a lot of temporary coding and documenting what could change once Obj-C was gone.

  • Xcode automatically generates a bridging header to expose Objc-C classes to Swift, but it makes assumptions about naming conventions that can create mismatches. The file can't be manually edited and sometimes took several clean/build cycles to get it to update.

  • A Swift class can inherit from an Obj-C class, but an Obj-C class can't inherit from a Swift class. This meant I'd have to convert all the subclasses of a superclass first, then convert the superclass, then go back and make adjustments to all the subclasses, and repeat that cycle as I work up the tree.

This finally felt like too much time to spend on a temporary setup, and I decided just to push toward a complete conversion without further attention to interoperability. Not far into that, Xcode's real-time error checking (the red and yellow icons) gave up and left me with no help from the compiler ... so when everything was converted and I was able to try building again, I had 8000 compiler errors to deal with. But that's finally done and now I'm running and testing my 98% Swift app (I have a few small third-party utilities that I left alone for now).

The original poster compared his project to walking across the Sahara. I kept imagining my project as a 500 mile hike. The compiler giving up was like running out of water and then fixing all those errors was like doing the last 50 miles uphill in the mud. But I like hiking, so this metaphor kept me motivated. :-)

I have some small projects and when I convert those, I will do them all at once. I would say the smaller your project is, the less reason there is to mess with interoperability between the two languages.

By the way, my process was to convert a few files at a time with Swiftify, then manually clean them up line-by-line, sometimes at a rate of only 200 lines per hour. With all the cleanup, I'd estimate Swiftify cut the conversion time in half -- not amazing but still worthwhile. I have an Android version of the same project in Kotlin, and it was sometimes faster to copy and paste Kotlin code and tweak it to Swift than to convert from Objective-C because Kotlin and Swift are so similar.

Upvotes: 3

Mukesh Lokare
Mukesh Lokare

Reputation: 2189

Reference Swiftify

Step 1: Make sure you have latest version of Xcode (Recommended Xcode 11 & newer).

Step 2: Sign or Sign Up to site to download the app.

Step 3: Download and install Swiftify for Xcode.

Step 4: If the app is blocked from running, go to the Apple menu > System Preferences... > Security & Privacy > General tab. Under the section labeled "Allow applications downloaded from," select "Mac App Store and identified developers".

enter image description here

Step 5: Run “Swiftify for Xcode” from your Applications folder and enter the following API key:

Please, Sign In or Sign Up Free to get your own API key.

Step 6: If there’s nothing in the Editor menu, open System Preferences -> Extensions and put a checkmark next to “Swiftify for Xcode”.

enter image description here

Step 7: Run (or restart) Xcode and check the Editor -> Swiftify menu. enter image description here

Step 8: The new Finder extension allows you to convert files, folders and even ZIP archives with your projects using the Right-Click menu: enter image description here

Step 9: You can also use the Right-Click menu to convert code from most macOS text editor apps: enter image description here

Step 10: You can set a shortcut (Key Binding) for any command via Xcode -> Preferences -> Key Bindings. enter image description here

Upvotes: 3

Md. Ibrahim Hassan
Md. Ibrahim Hassan

Reputation: 5477

I recently converted SVProgressHUD to swift using Swiftify. The converted code can be found at here.

The major takeaways would be:

  1. To start the code conversion one file at a time maintaining interoperability with Objective C, that is the converted swift file should be interoperable with your existing Objective-C code.
  2. Pick a class which does not have subclasses and is simple.

Conversion Strategy Flowchart

The detailed conversion strategy can be found here.

Upvotes: 11

Sohel L.
Sohel L.

Reputation: 9540

Your approach of converting Objective-C to Swift is wrong! Apple also took time to adopt Swift completely in their frameworks and the news is in 2018, 85% of the frameworks are converted to Swift, so the point is they has also taken nearly 3 years to get it done!

The biggest problem is that Swift is still evolving and probably next year we might see "Swift 5.0". So what I suggest you to go via following way:

  1. Pick the latest version of Swift (i.e. 4.2).
  2. Rather than start converting complete project, adapt modular way.
  3. From your project, first of all start picking up smaller modules which don't affect app in any way and see that the "Swift" file works well with Objective-C (Your old code). Reference: How can I import Swift code to Objective-C?
  4. Once you are done with smaller ones, slowly start picking up big modules also you may find open source Swift libraries which are in Objective-C in your project.
  5. Besides, you can also build modules from scratch in the form of smaller projects and then just drag and drop in Objective-C project.
  6. Andreas Oetjen Suggestion: You might start by separating the class hierarchy, and convert one "subtree" after the other.

How the above Points help?

  1. You may find some unusual code or libraries lying around.
  2. You may end-up having clean code under proper structure
  3. You can use "Swift + Objective-C" as of now to make your app running smoothly and also giving updates regularly rather than waiting for the months to convert it completely.

Upvotes: 9

Related Questions