当在下面的代码中被块捕获时,变量'observer'未初始化。
初始化观察者的正确方法是什么,这样我就不会收到此语义警告?
[alert addAction:[UIAlertAction
actionWithTitle:NSLocalizedString(@"Go to Settings", nil)
style:UIAlertActionStyleDefault
handler:^(UIAlertAction *action) {
id observer = [NSNotificationCenter.defaultCenter
addObserverForName:UIApplicationWillEnterForegroundNotification
object:nil queue:nil
usingBlock:^(NSNotification *note) {
dispatch_async(dispatch_get_main_queue(), completion);
[NSNotificationCenter.defaultCenter removeObserver:observer];
}];
if (![UIApplication.sharedApplication openURL:settingsURL]) {
completion();
[NSNotificationCenter.defaultCenter removeObserver:observer];
}
}]];