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.
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Sign up/Sign in
With a free Kodeco account you can download source code, track your progress, bookmark, personalise your learner profile and more!
Create accountAlready a member of Kodeco? Sign in
Contents
Swift Language FAQ
25 mins
- The Basics
- I’m a beginner. Should I learn Objective-C, Swift, or both?
- I’ve been an Objective-C developer for YEARS. Am I now a beginner?
- Will iOS 8 and OS X Yosemite apps only use Swift?
- Does Swift work with other versions of iOS and OS X?
- Is Swift meant to replace Objective-C, or supplement it?
- What is a playground?
- Is the NDA lifted yet?
- How can I learn Swift?
- Will your future books and tutorials use Swift?
- Diving Right In
- Is there anything that Swift can do that Objective-C can’t, and vice-versa?
- Are there any APIs that don’t work with Swift?
- Where are my println() results in my Playground?
- How do I see those cool graphs of values in Playgrounds?
- How do you run the REPL?
- Can you use Swift to call your own Objective-C code or a third party library? If so, how?
- So, arrays can only contain one type of object? What if I want varied types?
- Is the same true for dictionaries? Are dictionaries also strongly typed?
- The Nitty Gritty
- Is there an equivalent to id in Swift?
- How do you do introspection in Swift? (e.g. equivalent of if ([obj isKindOfClass:[Foo class]]) { ... })?
- How do you put bitshifted values inside an enum in Swift? (i.e. MyVal = 1<<5)
- How does Swift work with Grand Central Dispatch?
- What about the internationalization macros from Objective-C?
- Do I need to worry about reference cycles?
- When should I use strong vs. weak vs. unowned references?
- Where are the semi-colons?!
- What are best practices with Swift coding style?
- What's Next?
- What is the future of Swift?
- How will CocoaPods adapt to Swift?
- More Questions?
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 receiveAnyObject
. - When an Objective-C API returns
nil
, Swift will receive anOptional
set to the valueNONE
which is Swift’s way of saying a variable isnil
. Because Swift variables must always contain a value, it uses theOptional
enum for any object returned from an Objective-C API being that there is no guarantee that an Objective-C method won’t returnnil
. - When an Objective-C API returns a collection it will be typed to
AnyObject
due to the inability to infer what type anNSArray
orNSDictionary
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 ofNSString
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.