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.

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.

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:

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

Figure A

swiftfigurea062214.png
 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

swiftfigureb062214.jpg
 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.

Request a Quote