Swift

Development of Swift started on July 2010 by Chris Lattner. Swift is a general-purpose, multi-paradigm, compiled programming language developed by Apple Inc. for iOS, macOS, watchOS, tvOS, and Linux.

Swift was introduced on June 2, 2014 at Apple’s developer conference WWDC. It is built with the LLVM compiler included in Xcode 6. The LLVM compiler is the next-generation compiler, introduced in Xcode 3.2 for Snow Leopard, based on the open source LLVM.org project.

On Dec 3 2015 Apple released Swift language as the open source project, under the Apache 2.0 license. Apple also launched the open source Swift community in support of open source Swift along with the below mentioned tools.

1)Swift.org: Dedicated to open source Swift community

2)Public source code repositories at github.com/apple

3)Swift native core libraries project with higher-level functionality above standard

4)Platform support for all Apple platforms as well as Linux

Currently Swift 3.0 is being used, Swift 3.0 superseded the previous versions Swift 2.0 and Swift 1.0. Swift 4.0 (beta) is also available.

Although Swift has simple and easy to learn syntax, but in reality, Swift, is a complex language. It embraces both object oriented and functional approaches, and it’s still evolving with each new release.

Swift 3.0 (what’s new)

examples: swift 2.0 code followed by swift 3.o code

1. In Swift 2.x and earlier, method name during call didn’t require label for the first method parameter. While in Swift 3.0 has made all labels required unless we request to omit the first one.

Earlier: fileData.writeToURL(“AnyFileUrlString”, atomically:false)

Swift3.0: fileData.write(to: “AnyFileUrlString”, options: [])

Earlier: arrayOfNames.removeAtIndex(5)

Swift3.0: arrayOfNames.remove(at: 5)

2. Swift 3.0 uses underscore to omit the first parameter label while making the method call

Earlier: textFieldShouldReturn(textField: UITextField) -> Bool

Swift3.0: func textFieldShouldReturn(_ textField: UITextField) -> Bool

3. Swift 3.0 omit the use of prefix “NS”

Earlier: NSFileManager.defaultManager().URLsForDirectory (.DocumentDirectory, inDomains: mainMask)

Swift3.0: FileManager.default.urls(for: .documentDirectory, in : .userDomainMask)

Earlier: NSJSONSerialization.JSONObjectWithData(jsonData, options: [])

Swift3.0: JSONSerialization.jsonObject(with: jsonData, options: [])

4. Public keyword got new definition

In earlier version of swift, the public keyword provides two useability

  1. Allows external modules to use the class/member
  2. Allows external modules to override the class/member

In Swift 3.0, the keyword “public” has got its definition trimed: now in Swift 3.0 public classes will only be accessable by other modules but cannot be overridable.

Swift3.0:

//ModuleA:

public class SuperclassA

public func foo () {} //accessable outside but not overridable .

//ModuleB:

import ModuleA

class SubclassB: SuperClassA

override func foo () {} //invalid because, cannot override the method defined public

5. “open” a new access specifer

An overridable class member may be declared open. Overridable class members includes properties, subscripts and methods. So in scenarios where you want the external accessbility and overridable properties for the methods, declare them open

Details about open method:

6. Change in way of dispatching

Earlier:

let actions = dispatch_queue_create(“com.test.action.queue”, nil)

dispatch_async(actions) {

print(“Hello World”)

}

Swift3.0:

let actions = DispatchQueue(label: “com.test.action.queue”)

actions.asynchronously {

print(“Hello World”)

}

7. Renaming of the types used in libdispatch following the guidelines of Swift3.0

Image: change in libdispatch names

8. Swift 3.0 comes with the replacement of UpperCamelCase with lowerCamelCase in enumerations and properties that start with an acronym.

Earlier: CGColor, CIColor.

Swift3.0: cgColor, ciColor

Earlier: let redShade = UIColor.red.CGColor

Swift3.0: let redShade = UIColor.red.cgColor

Enumerations too got changed to lowerCamelCase

Earlier: UIInterfaceOrientationMask.Portrait

Swift3.0 UIInterfaceOrientationMask.portrait

Earlier: NSTextAlignment.Left

Swift3.0 TextAlignment.left