Apple Introduces Swift 2.0

Last year, Apple introduced Swift, its very own programming language that focused on making it easier to build apps. Now, in an attempt  to make it more pleasant for developers, Apple is making Swift open-source.

The new Swift has many features to make your code more expressive as in the following:

  • Error handling: Swift 2.0 will have new language features and refinements that includes a new error handling model with familiar try, throw and catch keywords. It's also easy to create your own custom error types so you can describe error cases with clear, meaningful names. The new error model was designed to work seamlessly with NSError and the Cocoa frameworks. The new code now looks as in the following:

    1. func loadData() throws { }  
    2.   
    3. func test() {  
    4.    do {  
    5.       try loadData()  
    6.    } catch {  
    7.       print(error)  
    8.    }  

  • Syntax Improvements: With the improvements in the code syntax we can write more expressive code while improving consistency across the language. To make Swift code even cleaner and safer the new SDKs have been employed with the new Objective-C features such as generics and nullability annotation. The following are the sampling of enhancements in Swift 2.0:

    • Powerful Control flow with do, guard, defer and repeat.
    • Protocol Extensions and Default Implementations.
    • Keyword naming rules unified for functions and methods.
    • Extended pattern matching to work in if clauses and for loops.

    The powerful migrator that is included in Xcode 7 will help us to convert our application and playground code to work with the latest syntax improvements in Swift 2.0.
  • Support for Availability Checking: To get access to the latest features, documentation and API changes it is always suggested to use the latest SDKs available. Using Swift 2.0, it is easy to build the best possible app for each target OS version with its builtin availability checking feature. The compiler will give you an error when using a new API version for a minimum target OS and a new keyword lets you wrap blocks of code in a conditional version check to run only on specific OS releases.

The modern Swift is the result of the latest research on programming languages across Apple platforms. Named parameters brought forward from Objective-C are expressed in a clean syntax that makes APIs even easier to read and maintain. Inferred type makes code much cleaner without any mistakes, whereas modules eliminate headers and provide namespaces. Memory is managed automatically in Swift 2.0. In Swift 2.0 there is no need to even type semi-colons. All these modern Swift 2.0 features makes the language easier and fun to use.

  1. extension String {  
  2.    var banana : String {  
  3.       let shortName = String(dropFirst(characters))  
  4.       return "\(self) \(self) Bo B\(shortName) Banana Fana Fo F\(shortName)"  
  5.    }  

let bananaName = "Jimmy".banana

Swift has many other features that make our code more expressive and easy to understand.

  • Generics
  • Fast and concise iteration over a range or collection.
  • Structs that support methods, extensions and protocols.
  • Native error handling using try/catch/throw.
  • Tuples and multiple return values.
  • Functional programming patterns, eg., map and filter.

Interactive Playgrounds

Playgrounds make writing Swift code incredibly simple and fun. When we type a line of code the result appears immediately that we can directly pin below. Using the Xcode 7, playground can contain comments that use text with bold, italic and bullets lists in addition to embedded images and links. Playground lets you:

  • Share curriculum to teach programming with beautiful text and interactive code.
  • Design a new algorithm and watch its results every step of the way.
  • Create new tests and verify they work before promoting into your test suite.
  • Experiment with new APIs to hone your Swift coding skills.
  • Turn your experiments into documentation with example code that runs within the playground.


The following are the main safety concerns that Swift is designed for:

  • Swift 2.0 eliminates classes of unsafe code.
  • In Swift 2.0, variables are initialized before use.
  • In Swift 2.0 memory is managed automatically.
  • In Swift 2.0, arrays and integers are checked for overflow.
  • By default the Swift objects can never be nil. In fact, the swift complier will stop you from trying to make or use a nil object with a compile-time error.

Fast and Powerful

With the use of a high-performance LLVM compiler, Swift code is transformed into optimized native code that gets the most out of the modern hardware. The syntax and standard library have also been tuned to make the most obvious way to write the code that performs faster than before.

Open-Source

The most important thing is that Apple is making Swift 2.0 open-source later this year and that will be available for OS X, iOS and Linux. It is the most awaited feature of Swift for developers. With the unique combination of elegance, power and safety, Swift has the opportunity to move the entire software industry forward. The company will reveal more information as the open-source release gets nearer.

Highlights of Open-Source Swift

  • Swift source code will be released under an OSI-approved permissive license.
  • Source code will include the Swift complier and standard library.

Up Next
    Ebook Download
    View all
    Learn
    View all