What’s the real difference between Swift 2 and Swift 3?

By ongraph
December 8, 2016 | 3828 Views

Latest Industry insights to keep you updated on the latest happenings.

 

We at OnGraph have been one of the early adopters of SWIFT and have been following & adopting developments very closely. We wrote about Swift Programming Practices long back and have been backing up with relevant info through various blog posts

Swift 3.0 is the latest iteration of Apple’s programming language. At WWDC 2016, Apple here revealed all the new features and major code changes it brought to all Swift developers. Though there are a lot of changes that has come through in Swift 3.0, but in this article we will just focus on differences between Swift 2 and Swift 3.

 

Getting Started

 

The goal of creating Swift was to create the best available language for uses ranging from system programming, to mobile and desktop apps. Most importantly, Swift was designed to make writing and maintaining correct programs easier keeping in view the convenience for developers. Alike, Swift 3 with significant updates simplified things for developers. And ofcourse rage over Swift 2 due to the following differences:

 

  1. API Design Guidelines

The design of commonly-used libraries creates a major impact on the overall feel of a programming language. A great library feels like an extension of the language itself. Whereas, consistency across libraries enhances the overall development experience. To elevate the process of construction of great Swift libraries, Swift 3 strictly focuses on defining a set of API design guidelines and to apply those design guidelines consistently.

 

There are several major pieces that are used to define Swift API Design Guidelines, and they together are intended to provide a more cohesive feel to Swift development. Those major pieces are:

 

  • Swift API Design Guidelines
  • Swift Standard Library
  • Imported Objective-C APIs
  • Swift Guidelines Checking
  • Swift 2 to Swift 3 Migrator
  1. Omit Needless Words

In December 2015, when Swift went open source, its new API guidelines contained three fateful words “omit needless words”. It introduces another huge raft of breaking changes in Swift 3, as it conveyed that methods with names that contained self-evident words now have those words removed.

For instance, Swift 2.2 has the coding like:

 

let blue = UIColor.blueColor()

 

let min = numbers.minElement()

 

attributedString.appendAttributedString(anotherString)

 

names.insert(“Jane”, atIndex: 0)

 

UIDevice.currentDevice()

 

 

Here you can identify the needless words like UIColors, that is sure a blue and is going to be a color so saying blueColor() is needless. That’s where Swift 3 has escaped such thing.

 

And, here is that same code in Swift 3:

 

let blue = UIColor.blue

 

let min = numbers.min()

 

attributedString.append(anotherString)

 

names.insert(“Jane”, at: 0)

 

UIDevice.current

 

It is easily visible that Swift 3 has made method names significantly shorter!

This change particularly has a lot of effect on strings, which had repetition all over the place. To better understand its demonstration, go through the example below where the first line of each pair is Swift 2.2 and the second is Swift 3.0:

 

” Hello

 

“.stringByTrimmingCharactersInSet(.whitespaceAndNewlineCharacterSet())

 

” Hello “.trimmingCharacters(in: .whitespacesAndNewlines)

 

“Taylor”.containsString(“ayl”)

 

“Taylor”.contains(“ayl”)

 

“1,2,3,4,5”.componentsSeparatedByString(“,”)

 

“1,2,3,4,5”.components(separatedBy: “,”)

 

myPath.stringByAppendingPathComponent(“file.txt”)

 

myPath.appendingPathComponent(“file.txt”)

 

“Hello, world”.stringByReplacingOccurrencesOfString(“Hello”, withString: “Goodbye”)

 

“Hello, world”.replacingOccurrences(of: “Hello”, with: “Goodbye”)

 

“Hello, world”.substringFromIndex(7)

 

“Hello, world”.substring(from: 7)

 

“Hello, world”.capitalizedString

 

“Hello, world”.capitalized

 

 

Reference – https://www.hackingwithswift.com/swift3

 

  1. Improvements to Tooling

Swift is a programming language and a large part of writing comprise the use of a development environment – which for Apple developers is likely to use Xcode! The change which has happened with the tooling has a large impact how you write Swift every day. Swift 3 fixes bugs in compiler and IDE. Updates also enable it to improve the precision of error and warning messages. And the most amazing thing is, Swift is getting faster with each release in how it runs and compiles:

 

  • By improving string hashing there was a 3x speedup in dictionaries of strings
  • By moving objects from the heap to the stack there was a 24x speedup (in some cases)
  • The compiler now caches multiple files at once (when doing whole module optimization)
  • Code size optimization has reduced the compiled size of Swift code. Apple’s demo Demobots reduced the compiled size to 77% of the original

Xcode is also learning how to think in native Swift:

 

  • When you right-click on an API method like sort() and jump to its definition, you used to be taken to a cryptic header file. Now, in Xcode 8, you see that sort() is an extension to Array like you would expect.
  • Swift Snapshots are like the nightly releases of Swift Evolution. They offer a chance to work with the new syntax before its fully baked into Xcode. Xcode 8 can load and run Swift Snapshots in playgrounds.

 

 

  1. Swifty importing of C functions

Swift maintains compatibility with a number of C language types and features as a part of its interoperability with Objective-C. There are also ways available to work with common C constructs and patterns with Swift, in case your code requires it.

Same like, Swift 3 bring forth attributes for C functions which enable library authors to specify new and beautiful ways their code should be imported into Swift. For instance, earlier the functions which start with “CGContext” now get mapped to properties methods on a CGContext object, which makes for much more idiomatic Swift. That means the hideous part that is CGContextSetFillColorWithColor() has finally been excised.

In order to demonstrate this, here’s an example in Swift 2.2:

 

let ctx = UIGraphicsGetCurrentContext()

 

let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)

 

CGContextSetFillColorWithColor(ctx, UIColor.redColor().CGColor)

 

CGContextSetStrokeColorWithColor(ctx, UIColor.blackColor().CGColor)

 

CGContextSetLineWidth(ctx, 10)

 

CGContextAddRect(ctx, rectangle)

 

CGContextDrawPath(ctx, .FillStroke)

 

UIGraphicsEndImageContext()

 

Now in Swift 3 the CGContext can be treated as an object that you can call methods on rather than repeating CGContext again and again. So, we can rewrite that code like this:

 

if let ctx = UIGraphicsGetCurrentContext() {

 

let rectangle = CGRect(x: 0, y: 0, width: 512, height: 512)

 

ctx.setFillColor(UIColor.red.cgColor)

 

ctx.setStrokeColor(UIColor.black.cgColor)

 

ctx.setLineWidth(10)

 

ctx.addRect(rectangle)

 

ctx.drawPath(using: .fillStroke)

 

UIGraphicsEndImageContext()

 

}

 

Why all this change?

 

Swift 3 introduces many changes, but the above ones are easy to read and most obvious. Though some of the changes are tiny but those hold potential of massive breakage. The mentioned changes definitely aid Apple’s Swift engineers to easily learn and make use it as fast as possible. Definitely, easy learning and fast practices ensures quick application development while saving time in coding. It also delights the clients as they will get their app development completed in less time as compared to earlier.

 

What change in Swift 3 is you most excited about? Let us know in comments.

Monthly industry insights to keep you updated on latest happenings

Follow us on Twitter
Follow us on Facebook
Follow us on Linkedin