What's new in Swift 4!

Swift 4

Apple has introduced an update to Swift during its WWDC. Lets discuss some of the new features introduced in Swift 4.

String
In Swift 4, String has been revised to be a collection again. This gives the feature to reverse, sort and count the characters.

Multi-Line String
As String literals allows you to create a sequence of characters within quotes (“), Swift 4 starting to support multi-line characters.
A multi-line string is enclosed within 3 quotes ("””).  A multi-line can also include unescaped double quotes, carriage return and line feeds.

To add a line feed, add a empty line to start and end of the multi-line string. The indentation defines the spacing between the lines.

Example,
let singleLineString = “I’m a single line string"

let multilineString = “""


I’m a 

Multi line string.

"""



Protocol Composition
Swift 4 allows developers to combine multiple protocols into a single type using protocol composition.

It will be like combining all the required protocols in the composition into a local temporary protocol. It is not defined as a new type, just creates a reference.

protocol Vehicle {
var brand: String { get }
}
protocol Model {
var model: String { get }
}
struct Car: Vehicle, Model {
var brand: String
var model: String
}
func purchasedNewCar(to brandName: Vehicle & Model) {
print(“Congratulations, Your new car \(celebrator.brand), model : \(celebrator.model)!")
}
let newCar = Car(brand: “BMW", model: “S Series")
purchasedNewCar(to: newCar)
// Prints "Congratulations, Your new car BMW, model : S Series!"


Extension Declaration
Extensions allows to extend the behaviour of the existing types. To define a extension,

extension  name : adopted protocols  {
    declarations
}

Extensions can have declarations like properties, instance methods, type methods, initialisers, typed properties, class, structure and enumerations.

In Swift 4, the declarations can’t be marked as 'final'.


Unicode 9 Support


Swift 4 added support to unicode 9. This allows the addition of new emoji’s and fixed some issues of earlier emoji’s.


Access Controls
Swift 3
private - properties created with ‘private’ control is visible only to the declared class.

Swift 4
private - properties will be visible to the declared class and extensions. Which means, any Extensions created for the class will also have access to this property within that same source file.

There are lot more features has been updated in Swift 4. Do check out Swift developer reference for more details,

Comments

Popular posts from this blog

Swift - Strings

Swift - Classes