错误:可写原子属性无法将合成的 setter/getter 与用户定义的 setter/getter 配对 [英] error: writable atomic property cannot pair a synthesized setter/getter with a user defined setter/getter
问题描述
我最近尝试编译一个旧的 Xcode 项目(以前编译得很好),现在我看到了很多这种形式的错误:
I recently tried to compile an older Xcode project (which used to compile just fine), and now I'm seeing a lot of errors of this form:
错误:可写原子属性someProperty"无法将合成的 setter/getter 与用户定义的 setter/getter 配对
导致这些错误的代码模式始终如下所示:
The code pattern which causes these errors always looks like this:
// Interface:
@property (retain) NSObject * someProperty;
// Implementation:
@synthesize someProperty; // to provide the getter
- (void)setSomeProperty:(NSObject *)newValue
{
//..
}
我知道为什么会产生错误.我告诉编译器合成我的属性访问器(getter 和 setter),然后立即手动覆盖 setter.这段代码总是有点臭.
I can see why the error is being generated. I tell the compiler to synthesize my property accessors (both getter and setter), and then immediately afterward I override the setter manually. That code has always smelled a little off.
那么,这样做的正确方法是什么?如果我使用 @dynamic
而不是 @synthesize
,我将不得不编写 getter.只有这样吗?
So, what is the proper way to do this? If I use @dynamic
instead of @synthesize
, I will have to write the getter as well. Is that the only way?
推荐答案
我遇到了同样的问题,经过一番研究,我对这个问题的结论如下:
I had the same problem and after doing a bit of research, here is my conclusion about this issue:
编译器警告您有关您声明为原子的 @property
(即通过省略 nonatomic
关键字),但您提供了如何同步访问的不完整实现到那个属性.
The compiler warns you about a @property
that you declared as atomic (i.e. by omitting the nonatomic
keyword), yet you provide an incomplete implementation of how to synchronize access to that property.
要使该警告消失:
如果您将 @property
声明为原子,请执行以下操作之一:
If you declare a @property
to be atomic then do one of the following:
- 使用
@dynamic
或; - 使用
@synthesize
并保留合成的 setter 和 getter 或; - 提供 setter 和 getter 的手动实现(不使用上述指令之一).
- use
@dynamic
or; - use
@synthesize
and keep the synthesized setter and getter or; - provide a manual implementation of both the setter and the getter (without using one of the above directives).
如果您使用 (nonatomic)
声明 @property
,那么您可以混合使用手动和合成的 getter 和 setter 实现.
If you declare the @property
with (nonatomic)
then you can mix manual and synthesized implementations of getters and setters.
更新:关于属性自动合成的说明
从 LLVM 4.0 开始,CLang 为不是 @dynamic
的声明属性提供自动合成.默认情况下,即使您省略了 @synthesize
,编译器也会为您提供 getter 和 setter 方法.但是,原子属性的规则仍然相同:要么让编译器同时提供getter和setter,要么自己实现它们both!
As of LLVM 4.0, CLang provides auto-synthesis for declared properties that are not @dynamic
. By default, even if you leave out the @synthesize
, the compiler will provide getter and setter methods for you. However, the rule for atomic properties is still the same: Either let the compiler provide both the getter and the setter, OR implement them both yourself!
这篇关于错误:可写原子属性无法将合成的 setter/getter 与用户定义的 setter/getter 配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!