Meet Swift: Apple's next-gen programming language that may replace Objective-C

Apple shocked longtime Mac and iOS developers at WWDC 2014 by introducing Swift, a new programming language that will likely replace Objective-C. Here's a quick guide to using Swift.

Image: Apple

Swift is the new, powerful programming language developed by Apple. It's designed to be safe and interoperable with existing code bases, and it takes a new approach to development on the Mac and iOS devices. Swift works with Xcode 6, and is compatible with modern versions of iOS and OS X.

Swift works with and is interoperable with Objective-C. It provides new syntax and syntactics that we've not seen before from Apple, and allows developers to experience a modern language without any of the legacy baggage of the C language, which has been a staple of programming since the '70s.

We give you the full details on how to use Swift in your development projects.

Can I use Swift now?

Swift is shipping with the latest version of Xcode 6 beta. You can create a Swift-based project by performing the following steps:

  1. Open Xcode.
  2. Click File | New | Project... .
  3. After selecting your project type, select Swift from the Language drop-down menu (Figure A).

Figure A

Image: Apple

Selecting Swift in Xcode couldn't be easier with the Language selection box.

Note: Xcode 6 defaults to Swift, so keep that in mind if you're still creating Objective-C projects for iOS or OS X.

Experimenting with Swift via the REPL

Swift includes something a bit new to Objective-C and compiled language users: a Read-Eval-Print-Loop (REPL). With the REPL, you can type in a line of code, have it evaluated for a result, and then have it returned to you. This means you can experiment with an idea by simply typing an expression in the command line — it's a pretty nifty feature.

A REPL is standard with interpreted languages such as Ruby, but many developers may not have experience with it. And, even though Swift has a REPL, it's still a compiled language.

Experimenting with Swift via the Xcode Playgrounds

Figure B

Image: Apple

Playgrounds let you visually see what is happening when working with visual elements such as games in SpriteKit and SceneKit, or standard images when doing animations.

Using the REPL, Xcode implements a new feature called Playgrounds for experimenting with code and seeing the results in real-time. A Playground is a file that can be included in your iOS or OS X projects, or live outside those projects as a standalone file. It gives you a place to work like a sketchpad when doing graphics, testing algorithms, and more.

Follow these steps to create a Playground:

  1. Open Xcode 6.
  2. Select File | New | File.
  3. Select either Source under either iOS or OS X templates.
  4. Choose the Playground file option.
  5. Click Next.

As you type into the code editor, the evaluation of the code is displayed in the right-hand sidebar, giving you the near-instant ability to see the output of any function or calculation.

Submitting Swift apps to the App Store

Using Swift requires creating and signing your binaries using Xcode 6; because of this, you cannot submit applications using Swift code to the iTunes or Mac App Stores at this time. When Yosemite and iOS 8 are released later this year, you will be able to begin submitting Swift binaries to the stores.

How do Swift and Objective-C differ?

The biggest and perhaps most drastic change between Objective-C and Swift is that the new language doesn't use any braces in the syntax. Swift is minimalist and tries to get away from distractions in the written code such as brackets, parenthesis around control statement conditionals, the ending-line semicolon, and more. Below are a few examples of how this code has changed.

Constants, variables, and properties... oh my

In Objective-C, you might see a constant defined like this, using the compiler declaration #define:

#define kMyConstantName 0.33

In Swift, this can easily be redefined using the let keyword, like this:

let kMyConstantName = 0.33

Using type inference, you no longer need to tell the compiler what type the constant should be; however, you should not confuse type inference with dynamically typed languages such as Ruby. Swift is still a strongly-typed language, but the compiler can automatically infer the type and type the variable or constant correctly.

Using this let keyword, it tells the compiler that this variable will not be changed during run time, and you will get a compile time error if you try to set another value with let-defined constants.

If you wish to use a variable, then the var keyword is what you're looking for. In Objective-C, you might see a string defined like this:

NSString *myString = @"This is a string.";

With Swift, you can write this code to do the same thing:

var myString = "This is a string."

With Swift you no longer need to define @properties declarations like you did in Objective-C. When you use the var keyword in your Swift file, it'll work just like an @property did in Objective-C, allowing you to set values from other classes when instantiating an object.

Blocks vs. closures

In Objective-C 2.0, Apple introduced the idea of blocks. While not revolutionary, this addition to the language helped modernize Objective-C. With Swift, Apple continues this modernization with closures.

Closures are essentially simplified blocks, and blocks and closures can be used interchangeably in Swift when dealing with Objective-C bridged code.

In Objective-C, you might have something like this when dealing with UIView animations, which uses blocks for completion handlers:

[UIView animateWithDuration: 0.33 animations:^{
    //Code here to handle animations

This is the same code in Swift using closures instead of blocks to interact with the same UIKit code written in Objective-C:

UIView.animateWithDuration(0.2, animations: {
    //Code here to handle animations

Closures can be very powerful, and can also be used to store a reusable code block in a constant or variable for use throughout your classes in Swift, just as you could do with blocks in Objective-C.

Can I use Swift with Objective-C and vice versa?

Objective-C code can be bridged into Swift, meaning that you can access all of your existing Objective-C classes, objects, and their properties with ease. You can use your Swift classes inside of your Objective-C classes to do things like open display a View Controller written in Swift through an Objective-C presenting view controller.

Learn more about Swift with these Apple resources

Apple has pulled out all of the stops to ensure that developers know exactly how to use Swift. I bet that by the next WWDC most if not all of the sessions will show API usage examples in Swift, so it's definitely worthwhile to spend the time to learn this new language. Here are some resources to get you started.


  • The Swift Programming Language: This book goes into the gritty details of the new language and how to use it. You won't find any mention of OS-specific details here — it's just about using the language and its new syntax. This should be your first look at Swift.
  • Using Swift with Cocoa and Objective-C: This book delves into the word of Cocoa and how to use existing Cocoa APIs in your applications. It also covers using existing Objective-C classes in your Swift code. This book should be read after you have a good understanding of the concepts in the first book.

Videos from WWDC sessions

  • Introduction to Swift covers the basics of Swift and how to define variables and constants, and how to use control statements.
  • Introduction to LLDB and the Swift REPL is an introduction to the debugging abilities in Swift and to the REPL that is included with the language.
  • Intermediate Swift covers object initialization, closures, and optionals, as well as the new switch statements that use pattern matching.
  • Integrating Swift with Objective-C covers how to integrate Swift partially with your existing Objective-C code (it'll be a while before production apps can go fully Swift-compatible).
  • Advanced Swift covers operator overloading, interpolation, and pattern matching. This also covers generics, and how to write expressive code with Swift.
  • Advanced Swift Debugging in LLDB: LLDB includes a lot of great new features for debugging Swift code, and this video goes into the advanced features like debugging for protocols, generics, optionals, and mixed-language code.

Also read

Disclaimer: TechRepublic, CNET, and ZDNet are CBS Interactive properties.


Cory Bohon is an indie developer specializing in iOS and OS X development. He runs a software company called Cocoa App and is also a developer at MartianCraft. He was introduced to technology at an early age and has been writing about his favorite te...

Editor's Picks

Free Newsletters, In your Inbox