Join Simon Allardice for an in-depth discussion in this video Swift and Objective-C interoperability, part of Comparing Swift and Objective-C.
- We have interoperability between these languages. I'm using this word interoperability in its most vague, generic, non-specific way. Simply that we want Swift to be able to use functionality that was written in Objective-C, and Objective-C to use functionality written in Swift. But I don't want to pretend that this is a level playing field here, and this interoperability is a fair and equitable 50/50 split. Oh, half the time we'll be writing Swift that talks to Objective-C, half the time we're writing Objective-C that talks to Swift. No, because that's neither true in our demand for this, nor in the capability of it.
Here's what I mean by that. To begin with I'm assuming that most people watching this are more interested in writing new Swift code that talks to existing Objective-C code whether that existing Objective-C is in your own projects, or it's an Apple framework or library originally written in Objective-C. Now that's not to say we won't ever need any other direction, we will. We might have an older Objective-C project and want to use a feature that's just been written in a new Swift project, and we can do that. But generally speaking, the demand leans more to writing new Swift code that's going to talk to existing Objective-C.
That's good because here's the thing. Swift can use Objective-C better than Objective-C can use Swift. Let me say that again. Swift can use Objective-C and understand it better than Objective-C can use and understand Swift. Now this really shouldn't be a surprise. First, because Swift was created knowing about Objective-C and knowing it has to deal with it. Interoperability was a core requirement of the Swift language. But, second, and more important when it comes to understanding the differences in how these languages interact, Swift was also created to be able to provide modern programming features that are not in Objective-C, and cannot easily be mapped to that language.
That means there are things we can do in Swift that Objective-C simply does not understand. We'll cover several of these shortly, but let me provide one specific example right now. In Swift, it is simple to define a method that returns a tuple, a grouped collection of values, say just a string and an integer bundled together. If you then ask, "How do I write Objective-C code "that calls this simple Swift method?" The answer is you don't, you can't, because the concept of a tuple doesn't exist in Objective-C.
There is no direct equivalent. This will be one of the things we can do in Swift that Objective-C doesn't understand, along with a few others like Swift generics, enumerations, and structures that are defined in Swift. We'll get to all of these, but something as simple as this should clear up any lingering misconceptions anybody might have that Swift is, as some woefully-uninformed bloggers have written, just basically Objective-C with a cleaner syntax. No, it really isn't. It's true that there are some ideas Swift takes from Objective-C, but the languages are fundamentally different.
However, the purpose of this course is to mainly focus on the ways these languages can interact with each other. So let's review the fundamental syntax differences, what they can both do that's written a little differently, and later we'll explore the things that one language can do that the other cannot.
Need a refresher on either language? Check out Swift Essential Training and Objective-C Essential Training.
- Exploring the interoperability of Swift and Objective-C
- Comparing language syntax
- Understanding how each language uses objects and methods
- Bridging arrays, dictionaries, and numbers
- Dealing with nil return types from Objective-C methods
- Working with NSError in Swift
- Working with Swift and Objective-C strings
- Adding Swift to an existing Objective-C project
- Inheriting between languages