Introduction to Algorithms and Data Structures in Swift 4: Get ready for programming job interviews. Write better, faster Swift code. (Swift Clinic Book 1) by Karoly Nyisztor

Introduction to Algorithms and Data Structures in Swift 4: Get ready for programming job interviews. Write better, faster Swift code. (Swift Clinic Book 1) by Karoly Nyisztor

Author:Karoly Nyisztor [Nyisztor, Karoly]
Language: eng
Format: epub
Published: 2018-02-10T23:00:00+00:00


To push it even further, let’s create a custom safe index operator:

// Array extension for index bounds check

extension Array {

subscript (safe index: Index) -> Element? {

return indices.contains(index) ? self[index] : nil

}

}

We implement the operator in an Array extension. Extensions and custom operators are really powerful. They let you add operators to existing types without modifying their code.

Using the newly created operator feels just as natural as using any built-in operator:

let emptyArray = [Int]()

let t = emptyArray[safe: index]

print(t ?? "no value at index \(index)")



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.