From Objective-C to Swift: Introduction

At the WWDC 2014, Apple unveiled Swift as a complete replacement for Objective-C, the recommended programming language for developing for the Mac OS X and iOS platforms since Apple bought NextStep in 1997.

While you can still use Objective-C for all development, knowing Apple, it makes sense to start using Swift as soon as possible.

The swift is a very fast bird with a recorded max speed of 171 km/h (106 mph)
The swift is a very fast bird with a recorded max speed of 171 km/h (106 mph)

Apple has provided excellent documentation for the new language on The main Swift page, but reading through all that may take some time. The point of these two articles is to enable you to jump in and start using Swift right away, while still knowing about some of the more interesting features. Where applicable, we will point you to Apple’s documentation for further insights.

Swift Example

Result:

Swift Migration

The above may look strange to Objective-C programmers, but Swift can look familiar too, like this code for a custom UIButton:

The same drawRect(), utilizing more Swift features:

Going a bit overboard:

For a list of associativity and precedence for the built in infix operators, see Binary Expressions.

 

Moving to Swift

So, I’m not going to waste too much of your time showing off random Swift code. The deal is to get you started writing your own fancy Swift code in a short while.

The Playground

Xcode 6 includes a file type called a Playground, where you can play with Swift as if it was a scripting language. You can create as many playgrounds in a project as you like.

A couple of tips:

  • Writing e.g.  map([1,2,3]) {$0*2} will probably not give you what you want. The result column just states “(4 times”), as something on the line was evaluated four times. Just assign it to something, and evaluate that:
  • Do not call -[NSObject addObserver]  in the Playground. It will crash.
  • To get a nice graph in the Timeline, hover over the result column (with text like “4 times”) and click the small circle.

Using Swift from Objective-C

Besides playgrounds, you may of course add real Swift source files to projects in Xcode 6. If you create a useful class in Swift that you would like to use from Objective-C, you should add the @objc attribute in front of the class declaration (if the parent wasn’t already Objective-C compatible, as e.g. imported UIKit classes), like so:

All applicable Swift stuff can then be used from your Objective-C code by including the project Swift header, named after your project:

And that’s all there is to it!

For more info, see Swift and Objective-C in the Same Project.

Using Objective-C from Swift

A bit more fun, and probably something you will use a lot more often, is accessing Objective-C (and C) stuff from your Swift source code.

A lot of existing Objective-C APIs are readily available. You just have to import it, like e.g.

For project specific APIs, you need the Bridging Header, which Xcode 6 will offer to create for you when you first add either a Swift file to an Objective-C project, of an Objective-C file to a Swift project. The header file is named after your project, like “MyProject-Bridging-Header.h” and should include all the #imports (and possibly declarations) that you want to be accessible from Swift.

For more information please refer to Using Swift with Cocoa and Objective-C.

Optionals

When working with Objective-C APIs, you will often have to interact with Optionals. These are needed as Swift is happily devoid of pointers, so denoting that a variable may point to nothing is achieved using a type that may either be nil or an actual value.

To work with Optionals, you use ?, !, and the “if let” statement:

When designing your own Swift APIs, you should of course only use Optionals when necessary.

For more information, please see Optional Chaining and The Optional Type.

Next up…

The next article will show you the main features of Swift, enabling you to read and understand Swift code like a pro.

Continue reading Swift Goodies

Leave a Reply

Close Menu