【发布时间】:2018-12-27 21:33:22
【问题描述】:
使用 Swift 3,我得到了这个,没有任何错误:
private var SessionRunningContext = 0
func addObservers() {
self.session.addObserver(self, forKeyPath: "running", options: .new, context: &SessionRunningContext)
}
func observeValue(forKeyPath keyPath: String?, of object: Any?, change: [NSKeyValueChangeKey : Any]?, context: UnsafeMutableRawPointer?) {
let newValue: AnyObject? = change![NSKeyValueChangeKey.newKey] as AnyObject?
switch context! {
case &SessionRunningContext:
// Do something
}
}
但是,当我在 iOS 12 Xcode Beta 上构建它时,我收到一条错误消息:
使用无关的'&'
对于这一行:
case &SessionRunningContext:
【问题讨论】:
-
有趣,看起来像一个临时错误。它已在 master 上修复(目前无法确认它是否已在最新的 Swift 4.2 快照中修复)。作为一种解决方法,您可以使用
case let x where x == &SessionRunningContext:。 -
... 或
switch context { case .some(&SessionRunningContext): ...或if context == &SessionRunningContext { ... -
(在最新的 4.2 快照中确实已修复)
-
已在 Xcode 10 beta 4 中修复。
标签: ios swift xcode unsafemutablepointer