Writing on Tablets Tech stuff from @yeltzland

Objective-C or Swift for new projects?

If I'm starting a new iOS project, which language should I choose?

I’ve been crazy busy over the last few months - a good problem to have, but it means I’ve had no time to blog on here 🙁

I’ve been switching between loads of different technologies to build mobile apps during that time. I thought it might be interesting to share my experiences switching between Objective-C and Swift, and the pros and cons of each.

Objective-C

It’d been a while since I’d worked in Objective-C, but I took on a project for one of my clients to enhance and modernise a couple of their apps which hadn’t been touched in a while.

To be honest the original code was pretty awful - they’d been let down by some contractors in the past and it took me a while to get my head around how the app worked.

Objective-C is notorious ugly to look at, with all those square brackets in the method calling, but once you get used to it I find it pretty productive to use.

I think knowledge of iOS APIs is by far the most important thing when developing on iOS, not the particulars how to type the code to call the API methods required.

The biggest plus point for Objective-C is the speed and stability of XCode and the compiler. After mostly doing Swift development in the last year or so, it was really nice to get fewer mysterious compiler problems.

I strongly suspect Apple still do most of their internal development using Objective-C, and until they move over wholesale to Swift there will be less internal pressure to improve XCode to handle Swift more reliably.

The main downside was that more and more of the code examples out there on the web - especially for the more modern APIs - are now in Swift. Less opportunities for “Stack Overflow copy-and-paste” development 😊

Swift

Most of my own apps are written in Swift now, and I’ve been doing the usual summer updates to keep them up to date for iOS 11 - and this year also for the challenge of iPhone X and the notch.

I must admit I do like Swift, and in particular the way it forces you to think hard about nullability of objects. This enforced type safety can be a bit painful at times (all the “!” and “?”s) but I trulythink you end up with more reliable code in the end.

However, as I mentioned above, XCode and the compiler are MUCH less reliable when using Swift. Quite often I’ve seen it do a poor job of recompiling changes, so you’re never 100% clear if you are seeing the latest code in the emulator. A restart of XCode usually fixes it, but that’s a real productivity hit.

The Swift compiler is also feels noticeably slower than the Objective-C compiler. All those small delays soon add up.

Also Swift is still a bit of a moving target, and once again we have Swift 4 and changes to the language. These seem to be getting fewer over releases, but I really hope the language gets to some sort of stability soon.

So which language will I choose for new projects?

I think I’ll still choose Swift for any new projects when I have a choice. Apple have strongly signalled this is their preferred language going forward, and as a language I do prefer it to Objective-C.

However after a few months back using Objective-C, I’ve really enjoyed the stability and speed of the tools. Let’s hope the Swift tools can improve to a similar state soon.

iOS Swift Objective-C