How to convert Giant project Objective C to Swift
Asked Answered
P

4

10

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?

Passifloraceous answered 23/10, 2018 at 6:36 Comment(6)
if it is running properly in objective C then the best thing is to leave it as it is and continue with objectiveC but if the requirement is to go for swift than you can do one thing while adding new module create it using swift.Rigney
another thing you can do is to break your project in module and start changing it to swift by module approachRigney
Don't convert it to Swift. Converting to Swift cannot be done automatically if it's done properly, you would have to implement everything again instead. Convert files when you are updating them and do that manually. That way your project will be converted to Swift one file at a time. There is nothing wrong with using legacy Obj-C.Tailspin
Keep both Objective-C & Swift. Create your new files in Swift. Redoing/convert Objective-C to Swift implies another bug fixes on each conversion. Do you have unit tests to do so? If the Objective-C code works, let it as such. Don't break to break code.Beefy
Worst thing is - You can not simply/directly convert the Objc Syntax to Swift Syntax. Best thing is - There are lots of projects running successfully which are gigantic like yours with mix of both the languages. Leave it as it is for now and start working on new modules using Swift. Later on start writing all the syntaxes of Objc files one by one if required. Figure out external dependency replacements of existing Objc version in Swift. Hopefully you will be able to build the entire project in Swift very soon..:)Hyperboloid
@SyedFarazHaiderZaidi Thanks for your comment, as you suggested to leave it as it is in Obj-C, project is keep updating and will run many years in future IA. So I want to convert it to Swift before Apple stop update Obj-C.Passifloraceous
L
11

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.

Longdrawn answered 6/12, 2018 at 19:21 Comment(2)
This is great. In addition I would suggest starting at the highest level (generally UI) you can and start working your way inward as obj-c can be called from swift.Zirconia
I would add a step "make sure the class is under unit test" before the "Convert or refactor class"Unexpressed
R
9

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.
Rammer answered 23/10, 2018 at 6:50 Comment(5)
You might start by separating the class hierarchy, and convert one "subtree" after the other. Fun fact: A module that doesn't affect the app can simply be kicked out without being converted to anything :-)Gathering
An obvious starting point would be converting the custom frameworks.Davie
IMO, custom frameworks are the least feasible as the starting point for the conversion, since they are (usually) rarely modified, and thus can be safely left in Objective-C. If you convert your project gradually, it makes sense to start with converting classes that you work with most. For a typical MVC project that will be View Controller, View and Model classes.Walking
@Sohil Thanks for your answer, my objective is to Convert my project in Swift before Apple stop updating Objective-C, and we don't know when Apple is going to stop updating Obj-C, I read few news and forums which have mixed thoughts on Obsoleting Obj-C, our project is keep updating every month with new requirements, so looking for some solution for conversion.Passifloraceous
@Passifloraceous I think it's quite not feasible to convert giant project in a day or weeks until and unless you have a big developer's team ;)Rammer
C
3

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.

Crinite answered 27/1, 2021 at 21:18 Comment(0)
A
3

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

Appressed answered 16/7, 2021 at 4:54 Comment(1)
Check appstore SwiftyFy app also, apps.apple.com/in/app/swiftify-for-xcode/id1183412116?mt=12Appressed

© 2022 - 2024 — McMap. All rights reserved.