Updated at: June 6, 2018

WWDC2018 kicked off in the wee hours yesterday (May 5), with a focus on iOS, macOS, tvOS and watchOS. As a developer, the first attention is to update the technology. Upgrade to iOS12 and Xcode10 on Mojave yesterday. This period of time will be updated intermittently. This article is about last night’s What’s New in Swift

Worth saying: Swift5 will have to wait until 2019

Faster Swift Debug Builds

The first impression of Xcode10 is Build Faster,

The official picture shows

In my actual tests, the compilation speed really improved a lot. Previous projects took 10-20 minutes to fully compile, but this time it took just over 4 minutes. And there are few barriers to moving to Swift4.2.

Xcode10 adds compilation Mode and Optimization Level

Copilation Mode:

  • Incremental update
  • Whole Module update

Optimization Level:

  • No optimization[-Onone]
  • Optimize for Speed[-O]
  • Optimize for Size[-Osize]

It’s worth noting that if your Optimization Level is set to Optimize for Size[-osize], the code Size is reduced by 10% to 30%, at a cost of about 5% less runtime performance

Small String

The size of the string will become smaller; on 64-bit machines, the string encoding is 16 bytes

New Language Features

New features for each Swift release can be viewed by developers on Github or Swift Evolution

1. A Derived Collection of Enum Cases

Making a link

Swift Evolution link

Prior to Swift4.2, we needed to do this by iterating through enumeration values


enum Gait {
    case walk
    case trot
    case canter
    case gallop
    
    // Define a collection to store all cases
    static var allCases: [Gait] = [.walk, .trot, .canter, .gallop]
}

for gait in Gait.allCases {
    print(gait)
}

Copy the code

If we add, delete or modify a case, we need to perform corresponding operation in allCases, which is very troublesome.

Swift 4.2 added CaseIterable to achieve the allCases effect.

enum Gait: CaseIterable {
    case walk
    case trot
    case canter
    case gallop
}

for gait in Gait.allCases {
    print(gait)
}

Copy the code

Further reading: Handling Future Enum Cases

2. Conditional conformances

Making a link

Swift Evolution link

Prior to Swift 4.2

After Swift 4.2, it is possible to compare elements within a collection

let coins = [[1.2], [3.6], [4.12]]
        
coins.contains([3.6]) // true

Copy the code

The official video lists these scenarios:

// Equatable
extension Optional: Equatable where Wrapped: Equatable {}
extension Array: Equatable where Element: Equatable {}
extension Dictionary: Equatable where Dictionary: Equatable {}

// Hashable
extension Optional: Hashable where Wrapped: Hashable {}
extension Array: Hashable where Element: Hashable {}
extension Dictionary: Hashable where Dictionary: Hashable {}

// Encodable
extension Optional: Encodable where Wrapped: Encodable {}
extension Array: Encodable where Element: Encodable {}
extension Dictionary: Encodable where Dictionary: Encodable {}

// Decodable
extension Optional: Decodable where Wrapped: Decodable {}
extension Array: Decodable where Element: Decodable {}
extension Dictionary: Decodable where Dictionary: Decodable {}

Copy the code

There is another interesting case: Conditional conformances allow composition

 // Int is Hashable
 // => Int? is Hashable
 //  => [Int?] is Hashable 

Copy the code

3. Syntheszed Equatable and Hashable

Github link Swift Evolution link

This new feature is implemented in Swift 4.1.

Prior to Swift4.1, if you wanted to compare two objects, the easiest way was to follow the Equatable protocol and then implement the methods defined by the protocol.


struct Person: Equatable {
    var name: String
    var age: Int
}

func= =(lhs: Person, rhs: Person) -> Bool {
    return lhs.name == rhs.name && lhs.age == rhs.age
}

Copy the code

Func == (LHS: Person, RHS: Person) -> Bool {}

After Swift 4.1, func == (LHS: Person, RHS: func == (LHS: Person, RHS: func == (LHS: Person, RHS: Person) -> Bool {} This method compares object properties one by one.

This is also appropriate for Hashable. Public var hashValue: Int {get} Public var hashValue: Int {get}

struct Person: Equatable.Hashable {
    var name: String
    var age: Int
}

let p1 = Person(name: "xiaoming", age: 12)
let p2 = Person(name: "xiaoming", age: 12)
let p3 = Person(name: "xiaohong", age: 12)
let p4 = Person(name: "xiaoming", age: 15)

print(p1 == p2) // true
print(p1 == p3) // false
print(p3 == p4) // false

print(p1.hashValue == p2.hashValue) // true
print(p1.hashValue == p3.hashValue) // false
print(p3.hashValue == p4.hashValue) // false

print(p1.name.hashValue == p2.name.hashValue) // true
print(p1.age.hashValue == p3.age.hashValue) // true
print(p1.name.hashValue == p4.name.hashValue) // true


Copy the code

4. Hashable Enhancements

Github link Swift Evolution link

The Hashable protocol has a new method

public protocol Hashable : Equatable {

    public var hashValue: Int { get }

    public func hash(into hasher: inout Hasher)
}

Copy the code

5.Random Number Generation

Making [Swift Evolution links] (HTTP: / / https://apple.github.io/swift-evolution/#? search=0202

6.Adding a build configuration INport Test

Github link Swift Evolution link

7.Target environment platform condition

Github link Swift Evolution link

8. Implicitly Unwrapped Optionals

swift.org/blog/iuo/

9. Enforcing Exclusive Access to Memory

Github link Swift Evolution link

Swift, all the new language features on https://apple.github.io/swift-evolution/ are listed, developers can click to view the original.