我什么时候应该明确使用@synthesize? [英] When should I use @synthesize explicitly?

查看:24
本文介绍了我什么时候应该明确使用@synthesize?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

据我所知,从 XCode 4.4 开始,@synthesize 将自动生成属性访问器.但是刚才我已经阅读了一个关于NSUndoManager 的代码示例,并且在代码中它注意到显式添加了@synthesize.喜欢:

As far as I know, since XCode 4.4 the @synthesize will auto-generate the property accessors. But just now I have read a sample of code about NSUndoManager, and in the code it noticed that the @synthesize is added explicitly. Like:

@interface RootViewController  ()

@property (nonatomic, strong) NSDateFormatter *dateFormatter;
@property (nonatomic, strong) NSUndoManager *undoManager;

@end

@implementation RootViewController
//Must explicitly synthesize this
@synthesize undoManager;

我现在很困惑...我应该什么时候将 @synthesize 明确添加到我的代码中?

I am feeling puzzled now... When should I add @synthesize explicitly to my code?

推荐答案

有很多答案,但也有很大的困惑.我会尽量安排一些订单(或增加混乱,我们会看到......)

There's a lot of answers, but also a big confusion. I'll try to put some order (or increase the mess, we'll see...)

  1. 让我们停止谈论 Xcode.Xcode 是一个 IDE.clang 是一个编译器.我们正在讨论的这个功能称为属性的自动合成,它是一个clang 支持的Objective-C 语言扩展,这是Xcode 使用的默认编译器.
    明确地说,如果您在 Xcode 中切换到 gcc,您将无法从该功能中受益(无论是 Xcode 版本).同样,如果您使用文本编辑器并从命令行使用 clang 进行编译,您将会.

  1. Let's stop talking about Xcode. Xcode is an IDE. clang is a compiler. This feature we are discussing is called autosynthesis of properties and it's an Objective-C language extension supported by clang, which is the default compiler used by Xcode.
    Just to make it clear, if you switch to gcc in Xcode, you won't benefit from this feature (regardless from the Xcode version.) In the same way if you use a text editor and compile using clang from the command line, you will.

感谢自动合成,您不需要显式合成属性,因为它会被编译器自动合成为

Thank to autosynthesis you don't need to explicitly synthesize the property as it will be automatically synthesized by the compiler as

@synthesize propertyName = _propertyName

但是,存在一些例外情况:

However, a few exceptions exist:

  • 具有自定义 getter 和 setter 的读写属性

当同时提供 getter 和 setter 自定义实现时,不会自动合成该属性

when providing both a getter and setter custom implementation, the property won't be automatically synthesized

带有自定义 getter 的只读属性

当为只读属性提供自定义 getter 实现时,这不会自动合成

when providing a custom getter implementation for a readonly property, this won't be automatically synthesized

@dynamic

当使用@dynamic propertyName时,属性不会被自动合成(很明显,因为@dynamic@synthesize是相互的独家)

when using @dynamic propertyName, the property won't be automatically synthesized (pretty obvious, since @dynamic and @synthesize are mutually exclusive)

在@protocol 中声明的属性

符合协议时,协议定义的任何属性都不会自动合成

when conforming to a protocol, any property the protocol defines won't be automatically synthesized

在类别中声明的属性

这是编译器不会自动插入@synthesize 指令的情况,但也不能手动合成此属性.虽然类别可以声明属性,但它们根本无法合成,因为类别不能创建变量.为了完整起见,我将补充说,仍然可以使用目标伪造属性合成-C 运行时.

this is a case in which the @synthesize directive is not automatically inserted by the compiler, but this properties cannot be manually synthesized either. While categories can declare properties, they cannot be synthesized at all, since categories cannot create ivars. For the sake of completeness, I'll add that's it's still possible to fake the property synthesis using the Objective-C runtime.

覆盖的属性(自 clang-600.0.51 以来的新特性,随 Xcode 6 一起提供,感谢 Marc Schlüpmann)

overridden properties (new since clang-600.0.51, shipping with Xcode 6, thanks Marc Schlüpmann)

当你覆盖超类的一个属性时,你必须显式合成它

when you override a property of a superclass, you must explicitly synthesize it

值得注意的是,合成属性会自动合成支持的 ivar,因此如果缺少属性合成,除非明确声明,否则 ivar 也会丢失.

It's worth noting that synthesizing a property automatically synthesize the backing ivar, so if the property synthesis is missing, the ivar will be missing too, unless explicitly declared.

除了最后三种情况,一般的理念是,无论何时您手动指定有关属性的所有信息(通过实现所有访问器方法或使用 @dynamic),编译器都会假设您想要完全控制该属性,它将禁用其上的自动合成.

Except for the last three cases, the general philosophy is that whenever you manually specify all the information about a property (by implementing all the accessor methods or using @dynamic) the compiler will assume you want full control over the property and it will disable the autosynthesis on it.

除了上面列出的情况外,显式@synthesize 的唯一其他用途是指定不同的 ivar 名称.但是约定很重要,所以我的建议是始终使用默认命名.

Apart from the cases that are listed above, the only other use of an explicit @synthesize would be to specify a different ivar name. However conventions are important, so my advice is to always use the default naming.

这篇关于我什么时候应该明确使用@synthesize?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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