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?
Update 8/5: Updated for Xcode6-beta 5.
Swift is Apple’s entirely new, modern, type-safe programming language for Cocoa development. Swift has been in development for 4 years, and was just announced this year at WWDC.
Swift takes different constructs that are loved from many languages including Objective-C, Rust, Haskell, Ruby, Python, C#, CLU and more.
Check out Matt Galloway’s recent post on some of the Swift language highlights for more details.
The syntax is much simpler and concise, which lowers the the barrier of entry to iOS development, and makes the process more delightful.
In this Swift Language FAQ, we will answer many frequently asked questions that we have seen around our forums, Twitter, email, and StackOverflow. We will also periodically update this FAQ with new questions that come along the way.
Note that some of these answers are opinions or speculation, so take them with a grain of salt. We’d also like to hear your opinions/comments, and we will update this FAQ appropriately based on your feedback.
The Basics
I’m a beginner. Should I learn Objective-C, Swift, or both?
In our opinion, it depends if you’re planning on working with another iOS company, or as an indie.
- If with an iOS company as a full-time iOS developer or consultant: It will be best if you learn both. This is because many iOS companies have existing code in Objective-C that you will need to understand, and some companies will not be transitioning to Swift right away. There are also tons of iOS libraries, tutorials, and sample projects written in Objective-C that you’ll need to understand. You’ll also need to learn Swift as things will be transitioning to Swift over time.
- If you’re an indie: If you only intend to use Swift from the start, you can theoretically get by only knowing Swift. But if you can spare the time, it is still a good idea to get an understanding of Objective-C so you can make use of the huge library of existing Objective-C resources.
This answer may change over the years as the landscape develops and Swift adoption grows. Eventually, knowing Objective-C may be akin to knowing COBOL ;]
I’ve been an Objective-C developer for YEARS. Am I now a beginner?
Yes and no. If you have been developing for Apple platforms for a while you still have a huge upper hand, since you are already familiar with Xcode and the Cocoa/Cocoa Touch APIs. Since learning Xcode and the thousands of Cocoa/Cocoa Touch APIs is much more time consuming than learning Swift, you should be in good shape.
Long story short, you will feel back at home once you are familiar with writing Swift code – and you should be able to pick up Swift fairly quickly.
Will iOS 8 and OS X Yosemite apps only use Swift?
No. Apple has built Swift in such a way that it fluently interoperates with Objective-C, and vice versa! Apple has not fully converted their Objective-C APIs and Frameworks to Swift, but you can still make use of them from your Swift code.
Only time will tell, but it is likely that many iOS and OS X shops will continue to rely on Objective-C for multiple years while adopting Swift.
Does Swift work with other versions of iOS and OS X?
Yes! Xcode 6 can compile Swift code for deployment targets of iOS 7 and higher as well as OS X 10.9 and higher. Apple actually developed the WWDC app in Swift which you can download from the App Store right now!
However, keep in mind that Apple does not permit builds to be submitted to the App Store from beta versions of Xcode. Therefore you will need to wait until the final version of Xcode 6 is released before getting your Swift code in the App Store.
Is Swift meant to replace Objective-C, or supplement it?
To quote Apple, “Objective-C is not going away, both Swift and Objective-C are first class citizens for doing Cocoa and Cocoa Touch development.”
So you can still use Objective-C. However, Apple seems to be encouraging you to use Swift for any new development, while not expecting you to go back and re-write all of your Objective-C code.
Although this is pure speculation, we are guessing Apple will also be moving away from Objective-C for future Framework and API development, and some day Objective-C may even be deprecated. So, hop aboard with the rest of the raywenderlich.com Team on the Swift train :]
What is a playground?
A playground is a file where you can write code and see the results immediately. They are really great for learning Swift or new APIs, for prototyping code, or for developing algorithms.
Be careful around your kids when you say that you’re going to the playground though. As Chris LaPollo learned, this is likely to make your kids cry after you sit down at the computer instead of taking them to the park!
Is the NDA lifted yet?
Update 30 June 2014: Our current understanding of the NDA is the following:
- You can release free articles/tutorials on Swift beta/iOS 8 beta. So we’ll be coming out with a bunch of free Swift tutorials here on out!
- You cannot post screenshots of Xcode 6 or iOS 8 beta. So in our tutorials, we will not be posting screenshots or videos of Xcode6 or iOS 8 beta until the NDA is lifted.
- You cannot release paid content on Swift beta/iOS 8 beta. So we are making our intro to Swift video tutorial series free, and will not release our Swift books until the NDA is lifted.
How can I learn Swift?
There are some great resources on learning Swift already:
- Apple’s Swift Programming book
- You can also read the book as an interactive playground in Xcode (Help\Documentation and API Reference\New Features in Xcode 6 Beta\Swift Language\The Swift Programming Language\A Swift Tour\Open Playground
- Swift videos from WWDC 2014
- We have three brand new Swift books – covering everything you need to know about Swift, iOS 8, and more
- We also have a Swift video tutorial series and a Swift Quick Start tutorial
- This Swift Language Cheat Sheet and Quick Reference may be helpful for your transition
We’ll also be coming out with a bunch of additional resources soon – check out the next question!