gpt4 book ai didi

ios - 在枚举之前使用 '\' 的观察者实现

转载 作者:行者123 更新时间:2023-11-28 11:37:55 27 4
gpt4 key购买 nike

最近我实现了一个进度 View 来显示网页上的加载过程。我阅读了一个示例(https://gist.github.com/fxm90/50d6c73d07c4d9755981b9bb4c5ab931)并进行了修改。但是部分代码对我来说并不清楚,正是

\.estimatedProgress

这是相关代码。

var webView: WKWebView!
// Progress view reflecting the current loading progress of the web view.
let progressView = UIProgressView(progressViewStyle: .default)
/// The observation object for the progress of the web view (we only receive notifications until it is deallocated).
private var estimatedProgressObserver: NSKeyValueObservation?

private func setupEstimatedProgressObserver() {
estimatedProgressObserver = webView.observe(\.estimatedProgress, options: [.new]) { [weak self] webView, _ in
self?.progressView.progress = Float(webView.estimatedProgress)
}
}

问题

  1. 为什么以及何时可以在枚举前使用字符“\”?
  2. 没有必要使用这种方式进行 deinit 或删除观察者吗?为什么?

最佳答案

\<Type>.<path>语法是 Swift Key-Path expression (而不是枚举)并且本质上是对某些路径的强类型引用 Type .当可以推断出 then 类型时,您可以省略它,因此表达式变为 \.path (在您的示例中,完整表达式为 \WKWebView.estimatedProgress )。

上面链接中有几个示例可以帮助您更好地理解这一点。

对于第二部分,观察只会持续estimatedProgressObserver。没有被释放(所以,只要有东西对它有很强的引用)。

关于ios - 在枚举之前使用 '\' 的观察者实现,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/54575667/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com