在我的应用程序中,我实现了一个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()
永远不会执行。知道为什么吗?