New features of Swift 2.0

Posted By : Aditya Kumar Sharma | 21-Mar-2016

Apple brought us Swift a new programming language for iOS and OS X both. Now they introduced Swift 2.0 which includes new features for error handling, support for availability checking and protocol extensions.

Here are some updates of the swift 2.0

  • Do-while -> repeat-while

The do-while is now changed to repeat-while. 

  • For-in where clauses

Introduction of where clause for the for-in  statement. We can define condition in a for using where  clause. For example:

for value in values where value>5000{ 
   }
 

It only prints the values which are greater than 5000

 

  • if-case Pattern Matching

We can use ‘if case’ to perform range matching. For example:

if case 0…99 = values {  
 }else if case 100…199 = values {  
 }
 

if case  can also be applied to pattern matching. For example:

if case(_,_,0<24,_) = clientDetail {
}else if case (_,_,_,let email) = clientDetail where email == “” {    
}
 

The first if case  clause checks if the client age is over 24. The underscore means some random values. The else if case is used to check if the email is empty.

 

  • Guard 

As we may notice, if we have to check more conditions, it will be nested with more conditions. Everything seems to look good programmatically but code gets messy as there are lots of nested conditions. This is where Guard statements comes in. For example:

Guard else{
//what to do if the condition are not met
}
 

If the condition, defined in the guard statement is not met, the code inside the else branch is executed. 

 

Error Handling

When developing an app or any programs we need to handle every possible conditions whether its good or bad. Swift 2.0 has new error handling conditions.

When we call a method that may fail, we generally pass it with NSError object as a pointer. If there is an error, the object will be set with the corresponding error. We then check if the error object is nil or not and then handle it accordingly.

  • Try/throw/catch

Swift 2.0 comes with an exception-like model using try-throw-catch keywords.

do {
let data = try NSURLConnection.sendSynchronousRequest(request, returningResponse: &response)
//Parse the data
} catch {
// handle error
}
 

Now we use do-catch statement to catch errors and handle them accordingly. As we may notice, we’ve put a try keyword in front of the method call. With the introduction of the new error handling model in Swift 2.0, some methods can throw errors to indicate failures. When we invoke a throwing method, you will need to put a try keyword in front of it.

Protocol Extensions

In Swift 1.2, we can use extensions to add new functionalities to an existing class, structure or enumeration. Swift 2.0 adds protocol extensions, and the standard library itself uses them extensively. Where we used to use global functions, Swift 2.0 now adds methods to common to make code more readable.For example:

protocol shoppingCart {
var items:[String]{get set}
func numberOfItems() -> Int 
}
 

We declare a protocol called shoppingCart which has a method declaration called numberOfItems. For any class that adopts the shoppingCart protocol, it must implement the method to return the total number of items in shopping cart.

Availability Checking

Using the latest SDKs ensures you get access to new features and information about platform changes. But sometimes you still need to target an older OS, and Swift makes doing so much easier and safer. The Swift compiler now shows an error when you use an API that is too new for your target OS, and #available blocks can safely wrap lines of code to only run when on the right OS versions.Here is an example:

@available(iOS 9,*)
    // implementations
 

We use  the #available keyword in a if statement. In the availability condition, we specify OS versions we want to verify. The asterick(*) is required and indicates that the if clause is executed on the minimum deployment target and any other versions of OS.

    Similarly, we can use guard  instead of if for checking API availability.

guard #available(iOS 8.4,OS X 10.10,*)else {
    // what to do if it doesn’t meet the minimum OS requirement
}

This is way to check if the class exists. In Swift 2.0, it has built-in support for checking API availability.

 

No more println()

In swift 2, we can only uase print() to write something to the output. Earlier it was println(). The print() function, by default, print message with a newline character. If we do not want output with newline, we can set the appendNewline parameter to false.For example:

print(“Swift 2.0”,appendNewline: false) 

 

Thanks

About Author

Author Image
Aditya Kumar Sharma

Aditya is a bright iOS developer, have knowledge of objective C, swift, swift 3, JSON, Core data and iPhone development. Apart from that he loves to travel and explore new things.

Request for Proposal

Name is required

Comment is required

Sending message..