WWDC 20 前你应该知道的 Swift 新特性(2):KeyPath 用做函数

Swift 5.2 中新增的另外一个的语言小特性是:KeyPath 用做函数。对于 KeyPath 还不熟悉的同窗能够先看一下这篇文章:SwiftUI 和 Swift 5.1 新特性(3) Key Path Member Lookupbash

在介绍 KeyPath 的时候咱们介绍过,一个 KeyPath<Root, Value> 的实例定义了从 Root 类型中获取 Value 类型的方式,它同时能被看做是 (Root) -> Value 类型的一个实例。函数

struct User {
    let name: String
}

users.map{$0.name}
users.map(\.name)
复制代码

上面的例子中 map 须要一个 (User) -> String 的函数实例,在 Swift 5.2 中,咱们能够直接传入 \User.name KeyPath,因为类型推断,能够进一步简化成\.name。它等价于users.map { $0[keyPath: \.name] }post

然而,非字面值的 KeyPath 是不支持的。ui

let keyPath = \User.name
users.map(keyPath)
复制代码

上面的代码会提示编译错误:Cannot convert value of type 'KeyPath<User, String>' to expected argument type '(User) throws -> T'spa

直接传不行,由于类型不匹配。为了解决这个问题,能够显式地转换成函数形式。code

let f: (User) -> String  = \.name
let f2 = \.name as (User) -> String
复制代码

编译器会生成相似的函数:get

let f: (User) -> String = { kp in { root in root[keyPath: kp] } }(\User.name)
复制代码

结语

Swift 5.2 中的语言特性介绍完了,让咱们一块儿期待 WWDC 20 以及 Swift 5.3 的新特性。编译器

相关文章
相关标签/搜索