Swift Language FAQ

Check out our Swift language FAQ, filled with answers to tons of common questions about Apple’s brand new programming language! By Chris Wagner.

Leave a rating/review
Save for later
Share
You are currently viewing page 2 of 4 of this article. Click here to view the first page.

Contents

Hide contents

Swift Language FAQ

25 mins

Will your future books and tutorials use Swift?

The short answer – yes! We’re fully committed to helping everyone transition to Swift.

The long answer:

  • iOS 8 by Tutorials: This will be written in Swift, to help everyone transition to this new language. We will also be providing the sample projects in Objective-C for those who are not quite ready to transition to Swift, and to aid those who wish to compare the two languages.
  • iOS Games by Tutorials: We will also be updating iOS Games by Tutorials to Swift – along with another major exciting update/change which we will announce soon.
  • Written tutorials: We will be using Swift in our future written tutorials once the NDA is clearly lifted. We will also be updating many of our older written tutorials to Swift. More on this later.
  • Video tutorials: We’ll be having a lot of video tutorials on Swift/iOS 8 soon, and also updating many of our video tutorials to Swift!
  • Some surprises…: We also have a few surprises up our sleeve – stay tuned! :]

Diving Right In

Is there anything that Swift can do that Objective-C can’t, and vice-versa?

Yes! Swift is a modern language that introduces many things that Objective-C does not support. Some of the big things include namespacing, optionals, tuples, generics, type inference and many more.

Objective-C also has some “features” that are not available in Swift like messaging nil.

It would be in your best interest to read the Using Swift with Cocoa and Objective-C Guide by Apple for more details – after reading this post of course!

Are there any APIs that don’t work with Swift?

At the time of writing this post, I am not aware of any. There are however some caveats on how to move things between Objective-C and Swift APIs. Here are some examples:

  • When an Objective-C API returns an id, Swift will receive AnyObject.
  • When an Objective-C API returns nil, Swift will receive an Optional set to the value NONE which is Swift’s way of saying a variable is nil. Because Swift variables must always contain a value, it uses the Optional enum for any object returned from an Objective-C API being that there is no guarantee that an Objective-C method won’t return nil.
  • When an Objective-C API returns a collection it will be typed to AnyObject due to the inability to infer what type an NSArray or NSDictionary stores. It is a good practice to downcast your collections when they are returned based on what you know of the API. Consider an Objective-C method that returns an array of NSString instances. Because you know that the returned array contains strings, you can safely downcast in the following manner.
    let fruits : [AnyObject] = // some Objective-C API that returns NSArray of NSStrings
    
    for fruit in fruits as String[] {
        println(fruit)
    }
    
  • When a Swift API returns a Tuple, Objective-C will receive… nothing! Since tuples are not supported in Objective-C, the method won’t be available to Objective-C code. There are a number of Swift constructs that Objective-C cannot support. These include…
    • Generics
    • Tuples
    • Enumerations defined in Swift
    • Structures defined in Swift
    • Top-level functions defined in Swift
    • Global variables defined in Swift
    • Typealiases defined in Swift
    • Swift-style variadics
    • Nested types
    • Curried functions
  • Generics
  • Tuples
  • Enumerations defined in Swift
  • Structures defined in Swift
  • Top-level functions defined in Swift
  • Global variables defined in Swift
  • Typealiases defined in Swift
  • Swift-style variadics
  • Nested types
  • Curried functions
let fruits : [AnyObject] = // some Objective-C API that returns NSArray of NSStrings

for fruit in fruits as String[] {
    println(fruit)
}

Where are my println() results in my Playground?

As of Xcode6-beta5, println() results now show up in the sidebar of Playgrounds. w00t! :]

How do I see those cool graphs of values in Playgrounds?

You can graph the results of values over time in Playgrounds, which can be really handy for visualizing algorithms.

To do this, enter some code that produces values over time like this in a playground:

for x in 1..<10 {
  x
}

In the sidebar, you'll see something like "9 times". Move your mouse over this line, and a + button will appear. Click this button (and make sure your Assistant Editor is open) and you should see the graph.

How do you run the REPL?

Run the following command in Terminal to tell it to use Xcode 6's command line tools.

sudo xcode-select -s /Applications/Xcode6-Beta5.app/Contents/Developer/

Then run the following to start the Swift REPL.

xcrun swift

When you are ready to exit you can type :exit or :quit. You can also use the CTRL+D keystroke.

Can you use Swift to call your own Objective-C code or a third party library? If so, how?

Yes! When you add your first .swift file to your Xcode Project you will be prompted to let Xcode create a bridging header file. In that header file you can import the Objective-C headers that you want to be visible to your Swift code.

Then, all of those classes will be available to your Swift code without further imports. You can use your custom Objective-C code with the same Swift syntax you use with system classes.

So, arrays can only contain one type of object? What if I want varied types?

In Swift you are highly encouraged to make strongly typed arrays that contain only one type of object, with syntax like this:

var goodArray: [String] = ["foo", "bar"]

That said, technically you can still create arrays that contain multiple types of objects. However, before you do this you should be asking yourself why you want to do this. In most cases it does not make the best sense and you can likely engineer your solution to be cleaner.

With that said, here's how you can create a Swift array with varying types of objects within it by using AnyObject:

var brokenArray: [AnyObject] = ["foo", 1, 12.23, true]

Is the same true for dictionaries? Are dictionaries also strongly typed?

Yes, but again you can get around this by using AnyObject. For dictionaries it often might make sense that not all of the values in your dictionary are of the same type. Consider a JSON response from a server that is represented as a dictionary:

var employee : Dictionary<String, AnyObject> = ["FirstName" : "Larry", "LastName" : "Rodgers", "Salary" : 65_000.00]

This dictionary contains two keys with String values and one key with a Double value. Although this is achievable, you should opt to create first class model objects to represent your data rather than relying on Dictionaries when possible.

Chris Wagner

Contributors

Chris Wagner

Author

Over 300 content creators. Join our team.