error:writable原子属性不能将合成的setter / getter与用户定义的setter / getter配对 [英] error: writable atomic property cannot pair a synthesized setter/getter with a user defined setter/getter

查看:484
本文介绍了error:writable原子属性不能将合成的setter / getter与用户定义的setter / getter配对的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近试图编译一个较旧的Xcode项目(用于编译只是罚款),现在我看到很多错误这种形式:


错误:可写原子属性someProperty不能将合成的setter / getter与用户定义的setter / getter配对


导致这些错误的代码模式总是如下所示:

  //接口:

@property(retain)NSObject * someProperty;

//实现:

@synthesize someProperty; //提供getter
- (void)setSomeProperty:(NSObject *)newValue
{
// ..
}

我可以看到为什么会生成错误。我告诉编译器合成我的属性访问器(getter和setter),然后立即手动覆盖setter。该代码总是闻到一点点。



那么,什么是正确的方法呢?如果我使用 @dynamic 而不是 @synthesize ,我也必须写getter。这是唯一的方法吗?

解决方案

我有同样的问题,经过一些研究,这里是我的结论问题:



编译器警告你一个 @property 你声明为原子的(即通过省略 nonatomic 关键字),但您提供如何同步对该属性的访问的不完整实现。



要使该警告消失:



如果您声明了 @property 为原子,然后执行以下操作之一:




  • 使用 @dynamic 或;

  • 使用 @synthesize 并保留合成的setter和getter或;

  • 提供手动实施 设置器和getter(不使用上述任何一个指令)



如果您使用(非原子)声明 @property ,那么您可以混合手动和合成的getter实现,



LLVM 4.0,CLang为不是 @dynamic 的已声明属性提供自动合成。默认情况下,即使你省略了 @synthesize ,编译器也会为你提供getter和setter方法。但是,原子属性的规则仍然相同:让编译器提供 getter和setter,或者自己实现


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:

error: writable atomic property 'someProperty' cannot pair a synthesized setter/getter with a user defined 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
{
    //..
}

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.

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:

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.

To make that warning disappear:

If you declare a @property to be atomic then do one of the following:

  • 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).

If you declare the @property with (nonatomic) then you can mix manual and synthesized implementations of getters and setters.

Update: A Note on Property Auto-Synthesis

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!

这篇关于error:writable原子属性不能将合成的setter / getter与用户定义的setter / getter配对的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆