Recently, I migrated parts of an iOS app from Objective-C to Swift. The app is for checking in attendees at events and uses the Salesforce Mobile SDK. I would like to share with you, some things I learned from the migration to Swift and updating to the latest Salesforce Mobile SDK.
First, the reasons (some are subjective) why I migrated parts of the app from Objective-C to Swift.
- The existing Objective-C code base contained a lot of technical debt.
- Salesforce Mobile SDK, which the app is tightly integrate with, is moving to Swift.
- Swift is a joy to write.
- Swift is easier to read.
- Swift is safer to use.
Tip 1 Link to heading
Before you migrate to Swift, update your Objective-C code to modern syntax and practices. Xcode can really help you, by suggesting changes or making the changes automatically.
Tip 2 Link to heading
When you are ready to start writing some Swift, start with something small. Perhaps a model or a small view.
Tip 3 Link to heading
Keep things Swifty. While a straight conversion from Objective-C to Swift, can work, there is often a better way to implement something in Swift. For example, instead of using a
for loop to iterate an array and do some filtering, use a
Tip 4 Link to heading
Replace the Objective-C
SFRestDelegate with the Swift
In keeping with Single Responsibility Principle, keep your RestClient separate from your View Controller. This will help you avoid massive view controllers.
Tip 5 Link to heading
To have your Swift code interoperable with your Objective-C code, you will need to prefix Swift class and properties with
Tip 6 Link to heading
In Swift, you can define optional properties, such as a nullable Integer. Such a type is not available in Objective-C. To access a nullable Integer, you will need a wrapper method. Create a getter or setter method in Swift and remember to prefix it with
Tip 7 Link to heading
Unit test your code for greater confidence when refactoring Objective-C to Swift. If possible, write unit tests before you start changing things.
Tip 8 Link to heading
Last, but not least, please keep in mind that you do not need migrate your entire project to Swift.