这可能是iOS 9.3版本中一个严重的bug。
当向
当向
[NSUserDefaults standardUserDefaults]
添加单个观察者时,我注意到响应方法-observeValueForKeyPath:ofObject:change:context:
会被多次调用。在下面的简单示例中,每次按下UIButton一次,observeValueForKeyPath就会触发两次。在更复杂的示例中,它甚至会触发更多次。这仅出现在iOS 9.3上(模拟器和设备都是如此)。这显然会对应用程序造成严重影响。还有其他人遇到了同样的问题吗?// ViewController.m (barebones, single view app)
- (void)viewDidLoad {
[super viewDidLoad];
NSLog(@"viewDidLoad");
[[NSUserDefaults standardUserDefaults] addObserver:self forKeyPath:@"SomeKey" options:NSKeyValueObservingOptionNew context:NULL];
}
- (IBAction)buttonPressed:(id)sender {
NSLog(@"buttonPressed");
[[NSUserDefaults standardUserDefaults] setInteger:1 forKey:@"SomeKey"];
}
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary<NSString *,id> *)change context:(void *)context {
NSLog(@"observeValueForKeyPath: %@", keyPath);
}
NSUserDefaults.h
吗?我在这个头文件中找不到你发布的评论。 - Borys Verebskyi