gpt4 book ai didi

ios - 使用 suiteName 检测 NSUserDefaults 的变化

转载 作者:IT王子 更新时间:2023-10-29 05:37:59 24 4
gpt4 key购买 nike

我创建了 appGroups 并使用该组设置了 NSUserDefaults。我有一个观察者,当值更改时不会触发该观察者。

let defaults = NSUserDefaults(suiteName: "group.daniesy.text")!            
defaults.addObserver(self, forKeyPath: "temp", options: NSKeyValueObservingOptions.New, context: nil)
defaults.setBool(!defaults.boolForKey("temp"), forKey: "temp")
defaults.synchronize()

override func observeValueForKeyPath(keyPath: String, ofObject object: AnyObject, change: [NSObject : AnyObject], context: UnsafeMutablePointer<Void>) {
println("changed")
}

但是,如果我忽略 appGroup 并使用 NSUserDefaults.standardUserDefaults(),它会按预期工作。知道为什么它不起作用吗?

最佳答案

macOS 10.12 和 iOS 10 的基础发行说明

https://developer.apple.com/library/archive/releasenotes/Miscellaneous/RN-Foundation-OSX10.12/index.html

Key-Value Observing and NSUserDefaults

In previous releases, KVO could only be used on the instance of NSUserDefaults returned by the +standardUserDefaults method. Additionally, changes from other processes (such as defaults(1), extensions, or other apps in an app group) were ignored by KVO. These limitations have both been corrected. Changes from other processes will be delivered asynchronously on the main queue, and ignore NSKeyValueObservingOptionPrior.

关于ios - 使用 suiteName 检测 NSUserDefaults 的变化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26599465/

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