- Reactive Programming with Swift 4
- Navdeep Singh
- 206字
- 2021-06-24 18:58:00
Smart key paths
Another important change introduced by Swift 4 is that of smarter key paths. Swift key paths are strongly typed and enforce a compile time check and remove a common runtime error.
You write a key path by starting with a backslash: `\Book.title`. Every type automatically gets a `[keyPath: …]` subscript to get or set the value at the specified key path:
struct Book {
var title = ""
let price : Float
}
let titleKeyPath = \Book.name
let mathsBook = Book(name: "Algebra", price: 10.50)
mathsBook[keyPath: titleKeyPath]
The value in the earlier mentioned keyPath is "Algebra".
The titleKeyPath object defines a citation to the name property. Then, it can be used as a subscript on that object. You can store and manipulate key paths. For example, you can append additional segments to a key path to drill down further. Key paths are composed of a root, and then you can drill down by following a combination of properties and subscripts.
If you change the variable of mathsBook from let to var, a specific property can also be modified through the keyPath subscript syntax:
mathsBook[keyPath: titleKeyPath] = "Trigonometry"
let newTitle = mathsBook[keyPath: titleKeyPath]
The value in the mentioned keyPath is "Trigonometry".
- Kali Linux Web Penetration Testing Cookbook
- Java加密與解密的藝術(第2版)
- Lua程序設計(第4版)
- Learn WebAssembly
- Learning AndEngine
- Android 應用案例開發大全(第3版)
- Big Data Analytics
- Learning jQuery(Fourth Edition)
- 匯編語言編程基礎:基于LoongArch
- Spring MVC+MyBatis開發從入門到項目實踐(超值版)
- Raspberry Pi Robotic Blueprints
- 代碼閱讀
- Struts 2.x權威指南
- Python編程快速上手2
- 關系數據庫與SQL Server 2012(第3版)