对于我没有合成的属性,我是否需要ARC关键字? [英] Do I need ARC keywords for properties that I don't synthesize?

查看:72
本文介绍了对于我没有合成的属性,我是否需要ARC关键字?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个我没有合成的属性,而是我自己创建了一个getter和setter。因此,我假设ARC关键字(强或弱)没有意义,所以我将它们消除。这在Xcode 4.3上工作得很好,但是当我的同事在XCode 4.2上打开它们时,编译器抱怨没有强/弱关键字,所以我指示他再次无意义地输入关键字。哪个是正确的(有或没有关键字)?

I have a property that I do not synthesize, instead I create a getter and setter myself. Therefore, the ARC keywords (strong or weak) have no meaning, I assume, so I eliminate them. This works fine on Xcode 4.3, but when my coworker opens them on XCode 4.2 the compiler complains that there is no strong/weak keyword, so I instructed him to meaninglessly enter the keyword back in again. Which is correct (with or without keywords)?

要明确:我有这样的属性 @property(nonatomic)NSString * foo 并在.m文件中实现 - (NSString *)foo - (void)setFoo:(NSString *) foo 并且不包括 @synthesize foo 。另一个相关细节是没有相应的iVar,而是属性与Core Data对象交互。这不会在XCode 4.2中编译,除非我在关键字中添加强或弱。

To be clear: I have a property like this @property (nonatomic) NSString *foo and in the .m file I implement -(NSString *)foo and -(void)setFoo:(NSString *)foo and do NOT include @synthesize foo. Another relevant detail is that there is no corresponding iVar, instead the properties interact with a Core Data object. This will not compile in XCode 4.2 unless I add strong or weak to the keywords.

编辑我想到了一个更相关的事情,其中​​一个属性是在一个协议上,我不知道这是否有所作为。

EDIT I thought of one more relevant thing, one of these properties is on a Protocol, I don't know if that makes a difference.

推荐答案

您引用的声明属性是可选的。引用文档:

The declared attributes that you are referencing are optional. To quote the documentation:


财产声明和实施

@property指令声明财产。可选的带括号的
属性集提供了有关存储
语义和属性的其他行为的其他详细信息 - 有关可能的值,请参阅属性声明属性。

Property Declaration and Implementation
The @property directive declares a property. An optional parenthesized set of attributes provides additional details about the storage semantics and other behaviors of the property - see "Property Declaration Attributes" for possible values.

属性声明属性

您可以使用表单 @property(属性[,attribute2,...)来装饰具有
属性的属性。 ..])

与方法类似,属性的作用域为其封闭接口
声明。对于使用以逗号分隔的列表
变量名的属性声明,属性属性适用于所有已命名的
属性。

Property Declaration Attributes
You can decorate a property with attributes by using the form @property(attribute [, attribute2, ...]). Like methods, properties are scoped to their enclosing interface declaration. For property declarations that use a comma-delimited list of variable names, the property attributes apply to all of the named properties.

如果你使用 @synthesize 指令告诉编译器创建
访问器方法(参见属性实现指令),它生成的
代码与关键字给出的规范。如果
你自己实现了访问器方法,你应该确保
符合规范(例如,如果指定copy,你必须
确保你在setter中复制输入值方法)。

If you use the @synthesize directive to tell the compiler to create the accessor methods (see "Property Implementation Directives"), the code it generates matches the specification given by the keywords. If you implement the accessor methods yourself, you should ensure that it matches the specification (for example, if you specify copy you must make sure that you do copy the input value in the setter method).

如果你然后使用 @dynamic 而不是 @synthesize 它告诉编译器你将编写自己的方法并防止它在找不到合适的方法时抱怨。

If you then use @dynamic instead of @synthesize it is telling the compiler that you will be writing your own methods and prevents it from complaining when it doesn't find suitable methods.

更多信息可以在这里找到

这篇关于对于我没有合成的属性,我是否需要ARC关键字?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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