Cheat Sheet

Swift For Dummies Cheat Sheet

From Swift For Dummies

By Jesse Feiler

Swift is Apple’s new programming language aimed for developers to use with iOS and OS X devices. Swift has been designed both to work alongside its predecessor, Objective-C, and to one day be Objective-C’s replacement. When you develop apps for iOS or OS X, you use the Xcode development tool (technically an Integrated Development Environment, or IDE), the Cocoa or Cocoa Touch frameworks, and a programming language —either Objective-C or Swift. Swift inherits much of Objective-C’s functionality — anyone comfortable with Objective-C’s types, collections, functions, classes, and flow control will be familiar with those structures in Swift, as well.

The Anatomy of a Swift Class

Classes are the heart of any object-oriented programming language. Unlike classes in Objective-C and some other languages, Swift’s classes need no header declaration. Instead, you get the entire class (or structure or enumeration) definition in a format like this:

class MyClass {
 var storedNumber: Int = 0
 init (myNumber storedNumber: Int) {
 self.storedNumber = storedNumber
 func simpleDescription() -> String {
  return String(self.storedNumber)
var test = MyClass(myNumber: 15)
println ("myNumber is " + test.simpleDescription());

The code in this example defines a class. Note the following characteristics:

  • It declares a stored property. It is an Int set initially to 0.

  • It creates an initializer that takes an Int as a parameter. The external name is myNumber and the internal name is storedNumber. The initializer sets the class instance value self.storedNumber using the storedNumber parameter (with the external name myNumber).

  • It declares a function called simpleDescription that returns a String representation of the stored number.

How to Update Xcode for a New Swift Release

New releases of Xcode (downloadable from for the beta and pre-release versions and from the Mac App Store for released versions) include documentation and APIs for new versions of Swift and the Cocoa and Cocoa Touch APIs. If you need to load older versions, go to the Downloads tab at Xcode→Preferences to review what needs to be downloaded. The unchecked items are available for download. The checked items have already been downloaded.


Working with both Swift and Objective-C

As of the start of 2015, almost all of the Cocoa and Cocoa Touch frameworks are written in Objective-C, and Swift can use them easily. (Of course it can; this was one of Swift’s design goals.) Even so, some aspects of the frameworks work well in Swift, but perhaps not as elegantly as you might like. In particular, these include the issues involved with passing pointers (used a great deal in the Objective-C frameworks) to and from Swift.

A related issue is the use of nil (as in nil pointers). Swift addresses this issue with the use of optional types — types such as Int?, which are related to non-optional types such as Int but which can accept the value of nil. (Sometimes optional types are called nullable types.) In Swift, you can unwrap an optional to deal directly with the underlying value that may be nil. You unwrap an optional value of type Int? by using an exclamation point, as in myOptional!.

Although developers inside Apple have been working with Swift for several years, most programmers only have half a year’s experience with the new language. In this short time, many developers have remarked on the fact that you still need to know a good bit about Objective-C to use Swift. How much you’ll need to know is hard to say, since virtually everyone who uses Swift today is already highly experienced with Objective-C. The experienced coders notice the pieces of Objective-C peeking through the Swift code.

If you’re starting from scratch, though, it’s fair to say you’ll need what linguists call a passive knowledge of Objective-C (meaning you can read and understand it) before you can develop an active knowledge of Swift (meaning you can read, understand, and write it).