被块捕获时变量未初始化

时间:2019-05-25 01:16:53

标签: ios objective-c

当在下面的代码中被块捕获时,变量'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];
            }
        }]];

0 个答案:

没有答案