What’s New in Swift 3

At WWDC 2016 Apple introduce Xcode 8 Beta and Swift 3, it brings major code changes to all Swift developers. Apple also included Swift 2.3 as a minor update bundled with Xcode 8. Swift 2.3 is same as of  Swift 2.2 but it support many of the new SDKs and features announced at WWDC. You can also check out this video of WWDC “Whats New in Swift 3” here.

 

Converting your code to Swift 3.

You’ll notice that practically every file needs changes! That’s because, almost all the Cocoa API names has been changed. Its not like they doesn’t exist, the API’s are the same, but there’s one name appropriate for Objective-C and one name appropriate for Swift. Like always Apple included a Migration Assistant Tool within Xcode 8 that make most of the changes in single go, though  there are a few areas that you need to touch up yourself.

The very first time the Assistant prompt  you to convert your code but if you missed or you want to convert later you can convert to either Swift 2.3 or Swift 3 anytime just navigate in Xcode to Edit > Convert > To Current Swift Syntax.  If you accidentally use the old API on a method call, the compiler will offer a Fix-It option that will help you use the latest API.

 

Whats new in Swift 3

 

Our beloved C-style for Loop

We use to write for loop like this below.

Its gone, Yes!!, there is no increment or decrement operator, all left is for-in loop.

But there is something new, one more alternative. for-each loop with closures and shorthand arguments

 

++ & — Operators

As you see the for  loop, there is no operator because they no longer exist, yes no ++ no — .

Compulsory First Parameter Label in Function

The first parameter in functions and methods now always has a label unless you request otherwise. Below are few example.

For Example :

And we will call it like :

 

Selectors as Strings No Longer Works

Like when we create custom button action via coding.

 

Grand Central Dispatch

My last tutorial was on GCD you can check it here. Its used for background tasks so that the UI thread will remain unaffected.

 

Swift 3 eliminates all the boilerplate code and redundant stuff by taking an object oriented approach.

Core Graphics

Core Graphics is a powerful drawing framework. Core Graphics was written in C and in the past used awkward function calls.

 

NS Prefix for Foundation Types all Gone

You can now use Calendar, Date instead of NSCalendar and NSDate. For example JSON Parsing, something we do everyday.

 

Naming Convention : Verbs vs Nouns

You can

choose a name based on the side effects or the actions taken. The rule of thumb is that if it includes a suffix like “-ed” or “-ing” then think of the method as a noun. A noun method returns a value. If it doesn’t have the suffix, then it is most likely an imperative verb. These “verb” methods perform the action on referenced memory. This is also known as modifying in place.

Enumeration Cases

Swift 3 treats enumeration cases like properties, so use lowerCamelCase naming instead of upperCamelCase notation for them:

Some More Swifty APIs

As you know Swift 3 is still developing, will keep you guys posted with latest Updates on Swift 3, Xcode 8, iOS 10, watchOS3.

Till then Happy Coding 🙂

Add a Comment

Your email address will not be published. Required fields are marked *

Time limit is exhausted. Please reload CAPTCHA.