在我的应用程序中,我实现了一个如下所示的 Preferences
类。
public class Preferences: Codable {
public static var sharedPreferences: Preferences = {
let preferences = Preferences()
return preferences
}()
public class func shared() -> Preferences {
return sharedPreferences
}
private let cloud: NSUbiquitousKeyValueStore = NSUbiquitousKeyValueStore.default
public init () {
NotificationCenter.default.addObserver(self, selector: #selector(keysDidChangeOnCloud(notification:)),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: nil)
self.cloud.synchronize()
}
@objc func keysDidChangeOnCloud(notification: Notification) {
print("CLOUD CHANGED")
}
}
我观察到键值存储发生变化。但 print()
从未被执行过。知道为什么吗?
最佳答案
我相信您需要像这样观察 NSUbiquitousKeyValueStore.default 对象:
NotificationCenter.default.addObserver(self, selector: #selector(keysDidChangeOnCloud(notification:)),
name: NSUbiquitousKeyValueStore.didChangeExternallyNotification,
object: NSUbiquitousKeyValueStore.default)
关于swift - 为什么我的 iCloud KVS Observer 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56316706/