gpt4 book ai didi

ios - 对于我不合成的属性,我是否需要 ARC 关键字?

转载 作者:可可西里 更新时间:2023-11-01 03:59:41 26 4
gpt4 key购买 nike

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

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

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

最佳答案

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

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

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,它会告诉编译器您将编写自己的方法,并防止它在未编写时报错找到合适的方法。

可以找到更多信息here .

关于ios - 对于我不合成的属性,我是否需要 ARC 关键字?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10022097/

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