KeyPaths in Swift

KeyPaths 是啥?

KeyPaths是对实例属性的一种引用,注意是对实例的属性的引用,而不是对属性值的引用,我们可以通过KeyPaths去获得或者设置属性的值,也可以在代码中传递KeyPaths;

struct Model {
    let title: String
    let desc: String
}

let model = Model(title: "model", desc: "this is model")
let titleKeyPath = \Model.title
let title = model[keyPath: titleKeyPath]
print(title)
// model

上面的代码中我们创建的 titleKeyPath: KeyPath<Model, String> 就是KeyPath, 然后我们通过titleKeyPath获取model实例的title属性值;

上面的例子可能看不出什么,下面给大家介绍下KeyPath 的强大;

代替闭包使用

如果我们想获得所有model的title,通常我们这么做:

let models = [model, model, model]
let titles = models.map { $0.title }

在Swift5.2中后 KeyPath可以代替闭包

let titles = models.map(\.title)

是不是看着舒服了不少

同样的我们还可以做一些有意思的扩展:

extension Sequence {
    func sorted<T: Comparable>(by keyPath: KeyPath<Element, T>) -> [Element] {
        return sorted { a, b in
            return a[keyPath: keyPath] < b[keyPath: keyPath]
        }
    }
}

达到类似协议封装的效果

struct ModelCellConfigurator {
    func configure(_ cell: UITableViewCell, for model: ProtocolModel) {
        cell.textLabel?.text = model.title
        cell.detailTextLabel?.text = model.desc
    }
}

当我们给cell赋值的时候,一般如果我们想要避免直接传递Model,我们可能会创建个协议,通过协议进行数据的传递,同样的KeyPath也可以达到类似的效果;

struct CellConfigurator<Model> {
    let titleKeyPath: KeyPath<Model, String>
    let descKeyPath: KeyPath<Model, String>

    func configure(_ cell: UITableViewCell, for model: Model) {
        cell.textLabel?.text = model[keyPath: titleKeyPath]
        cell.detailTextLabel?.text = model[keyPath: descKeyPath]
    }
}

当我们调用的时候就可以这样

let config1 = CellConfigurator<Model>(titleKeyPath: \.title, descKeyPath: \.desc)
let model = Model(title: "title", desc: "this is desc")
config1.configure(UITableViewCell(), for: model)

上面的代码就是我们通过KeyPath达到和Protocol一样隐藏Model的效果,使得代码更加通用,进一步解耦;

Converting to functions

class ListViewController {
    private var items = [Item]() { didSet { render() } }

    func loadItems() {
        loader.load { [weak self] items in
            self?.items = items
        }
    }
}

上面的代码,通常我们都需要将网络获取的数据赋值给本地数据源,然后本地数据源去通知刷新UI展示数据;

这个时候为了避免循环引用[weak self]是必不可少的,我们可以通过ReferenceWritableKeyPath来更加巧妙地解决这个问题;

func setter<Object: AnyObject, Value>(for object: Object, keyPath: ReferenceWritableKeyPath<Object, Value>) -> (Value) -> Void {
    return { [weak object] value in
        object?[keyPath: keyPath] = value
    }
}

我们改写原来的代码

class ListViewController {
    private var items = [Item]() { didSet { render() } }

    func loadItems() {
        loader.load(then: setter(for: self, keyPath: \.items))
    }
}

上面就是KeyPath部分功能的演示,运用熟练后KeyPath应该会成为一个利器;

引用:

The power of key paths in Swift

Keypaths in Swift Explained

©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容

  • 翻译自:What's New in Swift 5.1? Getting Started Swift 5.1 与 ...
    plantseeds阅读 1,184评论 0 0
  • *面试心声:其实这些题本人都没怎么背,但是在上海 两周半 面了大约10家 收到差不多3个offer,总结起来就是把...
    Dove_iOS阅读 27,239评论 30 472
  • Lightweight Generics(轻量级泛型)### OC类型声明用轻量级泛型规范等同于Swift内容的限...
    China_ly阅读 621评论 0 2
  • 久违的晴天,家长会。 家长大会开好到教室时,离放学已经没多少时间了。班主任说已经安排了三个家长分享经验。 放学铃声...
    飘雪儿5阅读 7,593评论 16 22
  • 创业是很多人的梦想,多少人为了理想和不甘选择了创业来实现自我价值,我就是其中一个。 创业后,我由女人变成了超人,什...
    亦宝宝阅读 1,895评论 4 1