iOSios开发iOS干货

在有了自动合成属性实例变量之后,@synthesize还有哪些使

2015-07-07  本文已影响1056人  alvin_ding

原文:

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

○ readwrite property with custom getter and setter

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

○ readonly property with custom getter

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

○ @dynamic

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

○ properties declared in a @protocol

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

○ properties declared in a category

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.

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

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.

A part from the cases I 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 propertyName = _propertyName

然而,有一些例外存在:

读写属性的自定义 getter 和 setter

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

只读属性的自定义getter

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

@dynamic

当使用@dynamic propertyName, 属性不会自动合成 (很明显,@dynamic 和 @synthesize 是互斥的)

@protocol 中定义的属性

当遵守了一个协议,任何协议定义的属性都不会自动合成

Category 中声明的属性

这种情况,@synthesize 指令不但不能被编译器自动生成,也不能手动合成属性。虽然category可以声明属性,但不能合成,因为category不能创建类实例变量ivars.为了完整起见,我还是加了这项,因为还是可以通过Objective-C运行时伪造合成属性

重写属性

当重写一个父类的属性,必须显示合成它。值得注意的是,合成一个属性会自动合成backing ivar,如果缺少属性合成,ivar也会消失,除非显示声明。

除了后三个情况,基本思想是当手动指定所有属性相关的信息(通过实现所有访问器方法或使用@dynamic),编译器会认为你想完全控制属性,他会禁用自动合成功能。

除了上面列举的情况,唯一使用显示@synthesize的是指定一个不同的实例变量名。依照惯例,我建议始终使用默认命名。

上一篇下一篇

猜你喜欢

热点阅读