Home > Articles > Programming > General Programming/Other Languages

Effective Objective-C and Swift: Q&A with Matt Galloway

  • Print
  • + Share This
Matt Galloway, author of Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS and OS X Programs, and Senior Acquisitions Editor Trina MacDonald discuss the Objective-C programming language and how Apple’s new Swift programming language will alter the landscape of future iOS application development.
From the author of

Trina Macdonald: For those with little no experience with iOS application development, perhaps you can tell us a bit about what Apple’s Swift language is and why it’s so important.

Matt Galloway: For as long as iOS development has been around, the language that you had to use was Objective-C. Apple has given the language a lot of love over the past few years, adding features like Automatic Reference Counting, which is an excellent memory management solution. They even added huge language features like blocks and literals. This brought it in line with many other modern languages. But the reality was that Objective-C was not going to scale.

At WWDC 2014, Apple announced a brand new language called Swift, which is to eventually replace Objective-C as the language used to develop iOS and Mac applications. Swift being brand new means that it is able to take the best features from other, existing languages. For example, those developers who are familiar with functional programming will find themselves at home with Swift. Many concepts of functional programming exist in Swift. That’s not to say you have to write your application in a functional way, but Swift provides the tools to do so if you wish.

Swift is important for all iOS and Mac developers because eventually Swift will replace Objective-C as the de facto language. Apple is going to start releasing system libraries that make use of Swift features. It may mean that certain APIs are only available if you’re using Swift. Therefore if you want to stay at the cutting edge of iOS and Mac development then you’re going to need to use Swift.

Trina: Swift’s been available now for programmers to work with since June. Now that you’ve spent some time working with the language, what do you think is the biggest challenge for Objective-C developers working now with Swift?

Matt: The biggest pain point has been the fact that Swift is constantly evolving. The language has already gone through a few major changes during its short public life to date. Syntax has changed, methods renamed and features added. This has all lead to having to go back over code written previously and work out how to make it work after the changes. The changes are often trivial, but it means that you have to spend some time bringing your codebase back into line after a new Xcode version is released.

We’ve only seen one subsequent non-beta release after 1.0. This came with Xcode 6.1 when Mac OS X 10.10 Yosemite was launched. Even this had a few important changes. One such change was that originally, Swift initializers had to return something, but now they can return nil. This is an often used feature of Objective-C’s initializers which was sorely missed in the first versions of Swift. It was great to see Apple respond so quickly.

The fact that Apple is letting the language evolve so quickly is a testament to it. It will certainly be able to stand the test of time if it is allowed to be molded over the first period of public release, just like any other product would.

Trina: How much and how well do Objective-C and Swift integrate with each other?

Matt: Apple has thought very well about interoperability of Objective-C and Swift. The core system libraries are all written in Objective-C, as it would be a lot of work to port these to Swift. They make heavy use Objective-C design patterns and conventions. Apple has made interoperating with these a breeze. Sometimes the syntax can get a little verbose, but you soon get the hang of it

Mixing Objective-C and Swift in a single project is also possible and also just as easy. It’s possible to call Objective-C code from Swift, and Swift code from Objective-C. However, some language features of Swift are unavailable to the Objective-C consumer, such as Swift struct and enum types. This means that until an application is fully ported to Swift, it won’t be able to make full use of Swift’s features.

The fact that interoperability is so easy means that porting an existing codebase can happen in stages. You don’t have to go and rewrite the entire app in one go. That would be very painful for a large codebase and potentially impossible if the rate new code is being written outpaces the rate at which code can be ported. Instead, you can take portions of your app and rewrite them one by one. Where you haven’t ported code yet you can use interoperability to bridge the gap. Once portions of the app are using Swift, you can start to use the Swift features that are not part of the Objective-C interoperability in those portions.

Trina: It seems like Apple is really making a push to bring Swift to the forefront of iOS development, especially when you consider this page on the Apple site. They emphasize that anyone can build an iOS app in Swift, even those who have never coded before. Do you think this means Objective-C developers should consider switching completely to Swift sooner rather than later?

Matt: Apple has certainly done a great job of emphasizing the simplicity of Swift. They’ve also released a lot of very good content such as the iBook which guides you through the language. It draws a lot of ideas from other languages, so many developers will feel somewhat at home with it. This makes it very accessible for all developers.

That said, Swift is not the only thing you need to know to develop iOS applications. Existing Objective-C developers will be very familiar with the patterns and intricacies of the system frameworks found in Cocoa. This knowledge is still just as relevant with Swift. Therefore I think that Objective-C developers should consider themselves already somewhat proficient in Swift.

For the most part, learning Swift is learning a new syntax. Once you’ve done that, you can write code that will feel at home with today’s system libraries. Though as I said before, there will come a time when system libraries are written in Swift and make full use of Swift’s features. When this time comes it will be important to be writing your apps in Swift. We can assume this is a long way off. It would be prudent to start learning Swift now though, so that you are ready when that day comes. And what better way to learn than by porting some existing code that you know inside out.

Trina: Are there elements of Objective-C programming that currently outperform Swift?

Matt: Certain things cannot be done in Swift that could be done in Objective-C. An example is deep interaction with the runtime, which is possible in Objective-C but not in Swift. For most developers this is a non-issue as they will never need to make use of runtime features. But it does show the direction that Swift is going in. It removes a lot of the flexibility of Objective-C. This can also be seen in the fact that Swift makes it a lot harder to use reflection. It exists to a certain extent, but not in the same way that it does in Objective-C.

The fact that Swift is not as flexible does mean that it can be more performant though. Limiting the language in certain ways means that optimizations can be made by the compiler which simply cannot happen in Objective-C. For example, Swift makes use of v-table based polymorphism as well as inlining. Objective-C cannot ever do this because it’s possible to rip methods out at runtime, change them or even change the type of an object.

Interacting with C APIs is also quite tricky in Swift. The type system of Swift makes it a little complicated sometimes to pass objects to and from C APIs. The syntax can become very verbose. It’s also currently impossible to create a C function pointer to a Swift function. This can make it tricky to work with some APIs which require this.

Where Swift lacks in flexibility, it makes up for in features that don’t exist in Objective-C.

Trina: One of the benefits of writing iOS apps in Swift is that the Swift language is supposed to be more safe. In your experience with the language do you find that to be the case?

Matt: Swift is said to be “safe by default.” One way in which it is safer is that types are much more strict than in Objective-C. When you have a variable of a certain type, you can guarantee that it is of that type. Whereas in Objective-C it’s possible to get types muddled up. A good example of this is with arrays. In Objective-C, arrays can contain any object. But in Swift, you declare what type can be stored in the array. The array carries the type information along with it. When you read an object back out of the array, you know what type it’s going to be.

Another example of safety is optionals. In Objective-C, sentinel values are often used to denote error, or empty state. The sentinel value is itself a valid value for the type, such as nil for an object type. However this can be problematic. Some APIs that return an integer use 0 to indicate error, some use -1 and some use the maximum integer. You need to know which one is being used as the sentinel. Optionals solve this problem in a much better way. An optional type can either be a value, or “none”. The latter being the case that replaces the sentinel. This is far superior because there is absolutely no ambiguity.

Overall, the fact that Swift is “safe by default” does make it a pleasure to work with in my experience.

Trina: Where you think Swift will be two years from now?

Matt: Swift certainly has a long road ahead. One day it will become the de facto language for iOS and Mac development. But that is longer than two years away in my opinion. I think that within two years we will see the first Swift enhanced system libraries. They will probably still have Objective-C counterparts, or at least use the interoperability functionality.

I think that there will be a lot of open source activity in Swift within the first two years as well. This will help pave the way for the future of the language. Patterns will start to emerge and the “right way” to do things will become apparent. This is an exciting time because we all have the ability to shape the future of the language.

While I think that some apps will be fully Swift in two years, I don’t believe the majority will be. There will be a lot of apps that use Swift to some extent, but I don’t think there will be many that are fully written in Swift. Those that are will likely be new apps that don’t have to port any existing Objective-C code.

The future is exciting for all iOS and Mac developers. These first few years really are an opportunity for all of us to mold the language and rethink how we develop our apps.

Be sure to check out Matt's book, Effective Objective-C 2.0: 52 Specific Ways to Improve Your iOS and OS X Programs, published by Addison-Wesley Professional.

  • + Share This
  • 🔖 Save To Your Account