r/ObjectiveC • u/ternaryop • Dec 11 '20
Best practices for migrating apps to Swift
My current company asked me to migrate three ObjC applications to Swift, the apps are very big and they are updated very often.
The apps use common code generating static libraries and I want to start from them.
At the end (of the first step) we will publish on MAS language mix apps, is this approach correct?
Any hints?
6
Upvotes
4
u/CodaFi Dec 11 '20 edited Dec 13 '20
Build a dependency graph of all your frameworks and libraries. Then identify the best place (usually closest to the bottom of the stack) to start migrating. You shouldn’t try to do this all at once, which is okay because Swift will let you use mixed-language targets so you can go piece by piece.
For frameworks that wish to mix Swift and ObjC, a compatibility header can be generated to allow you to transparently import Swift classes into ObjC frameworks higher up in the stack. Once you reach the app target, you use a bridging header to pull ObjC into Swift.
To go the other way (within the same target, including ObjC classes into Swift), use the umbrella header for the framework to import anything you want Swift to see.
The one thing I have to caution you about is going the other other way (within the same target, including Swift into ObjC). Therein lies a bizarre build path because in order for Swift to have generated the compatibility header, the target must already have built. So you get a chicken-and-egg problem in your install phase. If you absolutely must do this, write out the header yourself. It sounds fishy, I know, but it guarantees you a stable interface and if you mess up most of the time the linker will set you straight.
2
u/deepthoughtsby Dec 11 '20
This is just so timely, in case you missed this thread, it's a good read:
https://twitter.com/StanTwinB/status/1336893874904567808