Get Your Reading Glasses: Apple Publishes 500 Page Behemoth of a Guide for New Swift Programming Language for Cocoa and Cocoa Touch
Tuesday, June 3, 2014
Stuart Parkerson |
From a developer standpoint, one of the top news out of Apple’s WWDC is the new Swift programming language for Cocoa and Cocoa Touch, which works side-by-side with Objective-C.
Apple is touting the platform as a fast, high-performance LLVM compiler, where Swift code is transformed into optimized native code, tuned to get the most out of modern Mac, iPhone, and iPad hardware. The syntax and standard library are also tuned to make the most obvious way to write code also perform the best. Swift takes the best features from the C and Objective-C languages and includes low-level primitives such as types, flow control, and operators as well as object-oriented features such as classes, protocols, and generics, designed to fire up Cocoa and Cocoa Touch developers.
Developers can begin using Swift code immediately to implement new features in apps, or enhance existing ones. New Swift code co-exists along side existing Objective-C files in the same project. When iOS 8 and OS X Yosemite are released this fall, developers can submit apps that use Swift to the iOS App Store and Mac App Store.
And to get the inside scoop, Apple has provided a hefty guide featuring over 500 pages that provide a: tour of the language; detailed guide delving into each language feature; and a formal reference for the language.
Overview of Swift
With Swift, named parameters brought forward from Objective-C are expressed in a syntax that makes APIs in Swift easy to read and maintain. Inferred types make code cleaner and less prone to mistakes, while modules eliminate headers and provide namespaces. Memory is managed automatically, and there is no need to type semi-colons.
Swift features include:
- Closures unified with function pointers
- Tuples and multiple return values
- Generics
- Fast and concise iteration over a range or collection
- Structs that support methods, extensions, protocols.
- Functional programming patterns, e.g.: map and filter
With Swift Playgrounds, type a line of code and the result appears immediately. If code runs over time, for instance through a loop, developers can watch its progress in the timeline assistant. The timeline displays variables in a graph, draws each step when composing a view, and can play an animated SpriteKit scene. When the code is perfected in the playground, simply move that code into the project.
With Playgrounds, developers can:
- Design a new algorithm, watching its results every step of the way
- Create new tests, verifying they work before promoting into the test suite
- Experiment with new APIs to hone Swift coding skills
The debugging console in Xcode includes an interactive version of the Swift language built right in. Use Swift syntax to evaluate and interact with a running app, or write new code to see how it works in a script-like environment. It is available from within the Xcode console, or in Terminal.
Swift eliminates entire classes of unsafe code. Variables are always initialized before use, arrays and integers are checked for overflow, and memory is managed automatically. Syntax is tuned to make it easy to define intent. For example, simple three-character keywords define a variable (var) or constant (let).
The safe patterns in Swift are tuned for the Cocoa and Cocoa Touch APIs. Understanding and properly handling cases where objects are nil is fundamental to the frameworks, and Swift code makes this easy. Adding a single character can replace what used to be an entire line of code in Objective-C.
Using the high-performance LLVM compiler, Swift code is transformed into optimized native code, tuned to get the most out of modern Mac, iPhone, and iPad hardware. The syntax and standard library have also been tuned to make the most obvious way to write code also perform the best.
To get started with Swift, download Xcode 6 beta and follow the tutorials included in the documentation.
Read more: https://developer.apple.com/swift/
Become a subscriber of App Developer Magazine for just $5.99 a month and take advantage of all these perks.
MEMBERS GET ACCESS TO
- - Exclusive content from leaders in the industry
- - Q&A articles from industry leaders
- - Tips and tricks from the most successful developers weekly
- - Monthly issues, including all 90+ back-issues since 2012
- - Event discounts and early-bird signups
- - Gain insight from top achievers in the app store
- - Learn what tools to use, what SDK's to use, and more
Subscribe here