gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 02:29:35 25 4
gpt4 key购买 nike

昨天我将 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/

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