ios - 升级到 iOS 11.3 破坏了 __weak UIAlertAction

标签 ios objective-c crash uialertcontroller uialertaction

昨天我将 iPhone 6s 更新至 iOS 11.3。当我打开应用程序时,它立即崩溃了。我追踪到以下代码的崩溃,我发现我的"is"UIAlertAction 为零。

即使在我取出 __weak 声明后,代码也会运行并且不会崩溃,但是我的警报不会像以前那样在屏幕上弹出。我在各处使用了其中几个警报。

我的代码有问题还是这是一个合法的 11.3 iOS 错误? 有其他人更新到 11.3 后遇到过类似的崩溃吗? 这段代码已经完美工作了 1.5 年,最近没有任何问题 变化。

- (void) alertGotoAppSettings:(NSString *)title :(NSString *)msg :(UIViewController *)view
{
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:title
                                                                    message:msg
                                                             preferredStyle:UIAlertControllerStyleAlert];

    UIAlertAction __weak *yes = [UIAlertAction
                         actionWithTitle:LOC(@"Yes")
                         style:UIAlertActionStyleDefault
                         handler:^(UIAlertAction * action)
                         {
                             // Launch Settings for GPS
                             if (UIApplicationOpenSettingsURLString != nil) {
                                 NSURL *url = [NSURL URLWithString:UIApplicationOpenSettingsURLString];
                                 if (IS_IOS_10_OR_LATER) {
                                     [[UIApplication sharedApplication] openURL:url options:@{} completionHandler:^(BOOL success)
                                      {
                                      }];
                                 }
                                 else {
                                     [[UIApplication sharedApplication] openURL:[NSURL URLWithString:UIApplicationOpenSettingsURLString]];
                                 }
                             }
                         }];

    UIAlertAction __weak *no = [UIAlertAction
                         actionWithTitle:LOC(@"No")
                         style:UIAlertActionStyleCancel
                         handler:^(UIAlertAction * action)
                         {
                             dispatch_async(dispatch_get_main_queue(), ^{
                                 [alert dismissViewControllerAnimated:YES completion:nil];
                             });
                         }];

    [alert addAction:no];
    [alert addAction:yes]; <---- 'yes' is nil here

    UIAlertController __weak *weakAlert = alert;
    dispatch_async(dispatch_get_main_queue(), ^{
        UIAlertController *strongAlert = weakAlert;
        [view presentViewController:strongAlert animated:YES completion:nil];
    });
}

最佳答案

yesno 都不应该是 __weak。同样,weakAlert/strongAlert 模式应从此代码片段中删除。

仅当相关对象具有其他显式强引用,但您只是不希望代码建立另一个强引用时,才应使用弱引用。特别是,当存在强引用循环的风险时,您可以使用weak。但这里不存在这种潜在的强引用循环。坦率地说,在没有涉及其他显式强引用的情况下,将弱与局部变量结合使用根本没有意义。

底线,weak 表示“当没有剩余的强引用时,可以释放该对象,并且可以将该特定引用设置为nil”。但在这种情况下,由于您唯一的引用是弱引用,因此没有任何强引用。因此 ARC 可以自由地释放它们。

有问题的代码可能在过去有效(也许 ARC 对于何时释放对象更加保守),但在这种情况下删除这些弱引用是正确的。它们没有任何作用,并且使对象的范围变得模糊。

关于ios - 升级到 iOS 11.3 破坏了 __weak UIAlertAction,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49922966/

相关文章:

ios - 如何在导航 Controller 内的 Storyboard 中从appdelegate推送viewcontroller

ios - 如何在 AVMutableComposition 中同时为多个视频层制作动画?

iphone - 声音播放完毕后执行代码的最佳方式

iphone - 应用程序刚刚批准在 iPad 上崩溃

Android - 如何确保我的服务重新启动?

ios - 使用请求正文批量上传图片

ios - 表格 View 在其上方的 View 下滚动而不是与它一起滚动

iphone - 实时格式化美元金额

ios - UIBarButtonItem - 使其在仅文本时不可点击

php - 在大数据集上运行whereHas laravel方法会导致mysql崩溃