Swift 4.2 standard library changes

Swift 4.2 released

1. RangeReplaceableCollection collection add conditional delete method( Official text)

protocol RangeReplaceableCollection {
  /// Removes every element satisfying the given predicate from the collection.
  mutating func removeAll(where: (Iterator.Element) throws -> Bool) rethrows
}

extension RangeReplaceableCollection {
  mutating func removeAll(where: (Iterator.Element) throws -> Bool) rethrows {
    // default implementation similar to self = self.filter
  }
}

For example:

struct Person{
    let name:String?
    let age:Int
}

var arr = [Person(name: nil, age: 1),
                   Person(name: "b", age: 2),
                   Person(name: "c", age: 3),
                   Person(name: "d", age: 4)]
arr.removeAll {$0.age<2}

//[Person(name: Optional("b"), age: 2), Person(name: Optional("c"), age: 3), Person(name: Optional("d"), age: 4)]

Compared with the previous filter method, it is modified in the collection itself, and the method closure of filter is to return the collection whose closure is true.

 

2. Bool variable add switch method( Official text)

extension Bool {
  /// Equivalent to `someBool = !someBool`
  ///
  /// Useful when operating on long chains:
  ///
  ///    myVar.prop1.prop2.enabled.toggle()
  mutating func toggle() {
    self = !self
  }
}

Later, you can switch between model.model2?.model3.enabled.toggle()

3. Stochastic unification( Official text)

In the past, arc4random() was used for random numbers

Now

var myRandom = SystemRandomNumberGenerator()
let num = Int.random(in: 0..<10, using: &myRandom)

In addition, elements in a set can be randomly generated in a set

let arr = [1,4,6,9,11,12]
arr.randomElement()

You can also shuffle in a collection

var myRandom = SystemRandomNumberGenerator()
var arr = [1,4,6,9,11,12]
        
arr.shuffle(using: &myRandom)

 

4. Add last(where:) and lastIndex(where:) methods( Official text)

Add search index and find element methods from the back to the collection

let arr = ["aa","bbbb","ddddd","ccccc","kas"]
//Find elements from the back
arr.last{$0.count>4}//Return to ccccc
//Find index from the back
arr.lastIndex{$0.count>4}//Return to 3

5. Hashable enhancements( Official text)

A new hasher structure is added to the standard library. Each time hasher executes in the system, it will generate a different hash seed, which is randomly generated. Hasher provides hash state capture for Hashable generation.

struct Person:Hashable{
    let name:String
    let age:Int
}

The system will have a default implementation

var hashValue: Int{
     var hash = Hasher()//The value generated by this method is the same when the system is not restarted, and will change after restarting
     self.hash(into: &hash)
     return hash.finalize()
 }
 
 func hash(into hasher: inout Hasher){
     hasher.combine(name)
     hasher.combine(age)
 }

6. Sequence new all satisfaction algorithm( Official text)

let arr = [1,2,3,5,6,7,8,9]
arr.allSatisfy{$0>0}//true
arr.allSatisfy{$0>1}//false

 

Keywords: Swift

Added by anoopmail on Wed, 25 Dec 2019 20:58:23 +0200