xcode Auto-ARC 转换后:将保留的对象分配给不安全的属性;对象将在赋值后被释放

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/9752525/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 23:41:08  来源:igfitidea点击:

After Auto-ARC Conversion: Assigning retained object to unsafe property; object will be released after assignment

iosxcodeautomatic-ref-counting

提问by Kenny Wyland

I just converted an old project to ARC using Xcode's automatic refactoring.

我刚刚使用 Xcode 的自动重构将一个旧项目转换为 ARC。

@property (nonatomic, retain) NSMutableArray *cards;

was replaced by:

被替换为:

@property (nonatomic) NSMutableArray *cards;

This makes sense because what I've read is that "strong" is the default state. However, the following line is giving me the error in the title:

这是有道理的,因为我读到的是“强”是默认状态。但是,以下行给了我标题中的错误:

self.cards = [[NSMutableArray alloc] initWithCapacity:54];

The error is solved by adding strongback in where retain used to be:

通过strong在保留曾经所在的位置重新添加来解决错误:

@property (nonatomic, strong) NSMutableArray *cards;

However... if I need to go back and put strongin to every @property declaration that was retain... why did the ARC refactoring remove them all?

但是......如果我需要返回并放入strong每个@property声明retain......为什么ARC重构将它们全部删除?

回答by nschum

I've run into the same warning and opened an Technical Support Incident. The engineer verified that the default was changed from "assign" to "strong"for reasons of consistency within ARC.

我遇到了同样的警告并打开了一个技术支持事件。工程师确认出于 ARC 内的一致性原因,默认值已从“分配”更改为“强”

He said both the warning and the documentation are wrong and will be fixed. Until that is done, I would avoid the implicit defaultaltogether!

他说警告和文档都是错误的,将被修复。在完成之前,我会完全避免隐式默认值

Explicitly adding "strong" (as BJ Homer suggested) is a safe way to silence the warning and be compatible. But don't assume properties to be unretained by default. Always put "weak" or "assign" there, too.

明确添加“strong”(如 BJ Homer 建议的那样)是一种使警告静音并兼容的安全方法。但不要假设默认情况下不保留属性。始终将“弱”或“分配”放在那里。

Edit: The clang documentationnow officially documents this change. The warning has been fixed.

编辑:clang 文档现在正式记录了此更改。警告已修复

Edit 2: Xcode 4.4 apparently includes the fix.

编辑 2:Xcode 4.4 显然包含修复。

回答by mattjgalloway

It looks wrong that it converted nonatomic, retainto nonatomic. I've always seen it convert to nonatomic, strong. If you can produce a simple project that converts in the way you saw it then I suggest filing a radarwith it.

它转换nonatomic, retainnonatomic. 我一直看到它转换为nonatomic, strong. 如果您可以制作一个简单的项目,并按照您所看到的方式进行转换,那么我建议您将其归档

I assume by the way that you're using the latest Xcode.

我假设您使用的是最新的 Xcode。