Incorporating Swift in Objective-C projects
Since Apple released Swift, a lot of people were eager to use it, but some of us don’t start new projects that often. Newcomers don’t even learn Objective-C anymore. In our iOS community we never had a chance of writing in a new language. The last update I remember, was Modern Objective-C, which brought us few useful things, but the changes were small. We had
Convert to Modern Objective-C Syntax button, that mosty did the job. It’s also worth remembering that with Swift, we also got update for Obj-C. Also, remember the days of gcc? Since Apple compiler change, the things started to develop in a faster pace.
The Swift Decision
So, if you’re wondering whether you should port you app to Swift, this is my advice.
Wait for the ocasion. Look for:
- refactor planned
- new UI comming
- client demanding the change
- a lot of free time
In my case it was new UI. We made a decision to wait with Swift, and start incorporating it with the new UI. It went better then expected. What we thrived to achieve is to leave model data, communicators, managers in Obj-C, and make a Swift UI layer on top of it. The results are fantastic, we have divided “backend” and “frontend” of the app. A lot of functions, were moved from ViewControllers to Obj-C managers, along with relevant getters we needed for the UI.
I won’t get into details, because here is a great intro.
My notes on mixing Swift with Objective-C:
- If you have multiple targets, XCode it will generate a header for each target. In my case, I wanted one header for all targets.
- Swift can’t access class variables. So you’ll have to prepare getters.