In June 2014, Apple announced a new programming language called Swift which would replace Objective-C as the default programming language for building iOS, OS X, watchOS, and tvOS applications. Apple describes Swift as a powerful, interactive, intuitive, safe and fast programming language. The company further made Swift open source in December 2015. Apple also released Swift 2 in June 2015 with several new features and enhancements. These new features make it easier for iOS app developers to reduce innovative apps within a shorter amount of time.
Overview of New Features in Swift 2
New Error Handling Model: Swift 2 comes with a new error handling model that is both lightweight and readable. It helps programmers to understand why a function failed more clearly. The developers can further identify the errors using commonly used keywords like try, catch and throw. The current version of the programming language makes the error checking process safer. It allows users to specify the methods or functions that can throw errors using the throw keyword. The new error handling model can also be used with both Apple SDKs and other APIs using NSError.
Custom Error Types: Swift 2 allows programmers to define custom error types by implementing the ErrorType protocol on specific classes. The developers can further store the data for related errors in a single enum. While creating a class to be connected with an API, the programmers can simply create an APIErrors enum to hold the related errors. As an enum can hold extra data, the users can store data for several related errors.
Print Messages Automatically: While using Swift 1, the developers have to use the println(“the message”) function to display debug messages on the console. But the current version of Swift enables programmers to print messages on a line automatically without using the println(“the message”) function. The feature will make it easier for developers to print a message on a new line automatically.
Guard Statement: The latest version of Swift allows mobile app programmer to use new Guard statements. They can use the Guard statement to check if a particular condition is met and take the appropriate action if the condition is not met. Unlike If statements, the guard statements enable programmers to return an error message in case all conditions are not met.
Protocol Extensions: While using Swift 1, the programmers can add new functionalities to an existing class, structure or enumeration using extensions. Swift 2 allows developers to apply extensions additionally to various protocol types. Thus, the developers can take advantage of protocol extensions to add functions or properties to any class that conform a particular protocol.
API Availability Checking: Often developers have to build apps supporting different versions of iOS. While using Swift 1, the programmers did not have any option to check API availability through a standardized method. Swift 2 comes with built-in features to check the availability of API. The built-in availability checking feature will enable developers to execute a specific block of code only if the availability condition is met.
Availability: Despite using the latest SDKs, the iOS app developers often have to target multiple versions of the mobile operating system. So the programmers have to put additional time and effort to check if a particular API is compatible with different versions of iOS. Swift 2 makes it easier for them to check if an API is compatible with the target iOS version using a compiler. The Swift compiler will display an error message whenever an API is not supported by earlier versions of iOS. The developers can further use #available blocks to specify the blocks of code to be executed on specific versions of iOS.
Apple has already revealed the roadmap for Swift 3. The company is planning to release Swift 3 in fall 2016. The upcoming version of the programming language will allow developers to write portable Swift code that runs on multiple platforms. However, Apple is yet to specify which platforms Swift 3 will support. So the iOS app developers have to take advantage of the new features of Swift 2 till Apple releases the upgraded version.