@synthesize 和 @dynamic

Most Properties Are Backed by Instance Variables

You Can Customize Synthesized Instance Variable Names

  • As mentioned earlier, the default behavior for a writeable property is to use an instance variable called _propertyName.

  • If you wish to use a different name for the instance variable, you need to direct the compiler to synthesize the variable using the following syntax in your implementation:

@implementation YourClass
@synthesize propertyName = instanceVariableName;
...
@end

For example:@synthesize firstName = ivar_firstName;

In this case, the property will still be called firstName, and be accessible through firstName and setFirstName: accessor methods or dot syntax, but it will be backed by an instance variable called ivar_firstName.

Important: If you use @synthesize without specifying an instance variable name, like this:

@synthesize firstName;
  • the instance variable will bear the same name as the property.
  • In this example, the instance variable will also be called firstName, without an underscore.

https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html#//apple_ref/doc/uid/TP40011210-CH5-SW2

概述

声明的property, 如果没有特意指定synthesize的话,那么Objective-C就会自动的给你声明一个_开头的实例变量。
例如声明了一个叫param的property: @property (nonatomic, copy)NSString *param;
通过_param进行存取的话是不会调用该变量的setter或者getter方法的,声明的copy也就不会起作用,而通过self.param调用的话是会调用setter或者getter方法的,这样copy也就会起作用。

这样是通过自动的方式给你生成了一个_开头的变量,你当然可以不这样用,例如你在上面声明完property后,你可以在实现文件中再这么写
@synthesize param = param;
这样就没有了_param,取而代之的就是param,他们是一样的。最早之前这个@synthesize是需要我们自己去写的,所以根据个人习惯就会有不带_的实例变量,而后来苹果把@synthesize自动完成了,不需要我们去写了。

iOS 6 之后 LLVM 编译器引入property autosynthesis,即属性自动合成。换句话说,就是编译器会为每个 @property 添加 @synthesize ,如以下形式:

@synthesize propertyName = _propertyName;

1

这行代码会创造一个带下划线前缀的实例变量名,同时使用这个属性生成getter 和 setter 方法。

使用@synthesize 只有一个目的——给实例变量起个别名,或者说为同一个变量添加两个名字。

如果要阻止自动合成,记得使用 @dynamic 。经典的使用场景是你知道已经在某处实现了getter/setter 方法,而编译器不知道的情况。

此处我的理解是为了防止编译器使用自动合成生成新的setter/getter 会覆盖已经存在的旧的 setter/getter。

自动合成没有任何内存的含义,所以它根本没有连接到ARC。

如果你已经手动实现了 setter/getter方法,或着对 只读对象 实现了getter方法,那么自动合成不会产生任何影响。如果你在手动实现需要一个变量,只需声明它就可以了,不需要添加@synthesize来添加一个别名(尽管可以)。

还有一种情形需要使用 @synthesize ,就是当在 protocol 中声明并实现属性时。协议中声明的属性不会自动生成setter和getter,[UIApplicationDelegate window] 就是个典型的例子。

延伸阅读

iOS协议里面可以定义成员变量和属性吗?

代码探讨

情形1:不使用@synthesize,可以使用自动生成的带下划线的实例变量名

情形2:使用@synthesize为属性添加带下划线的别名,与不使用@synthesize相同

情形3:使用@synthesize为属性添加任意别名,此时使用自动生成的实例变量名将报错,只能使用指定的别名。

总结

1.@synthesize 的作用:是为属性添加一个实例变量名,或者说别名。同时会为该属性生成 setter/getter 方法。

2.禁止@synthesize:如果某属性已经在某处实现了自己的 setter/getter ,可以使用 @dynamic 来阻止 @synthesize 自动生成新的 setter/getter 覆盖。

3.内存管理:@synthesize 和 ARC 无关。

4.使用:一般情况下无需对属性添加 @synthesize ,但一些特殊情形仍然需要,例如protocol中声明的属性。

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

推荐阅读更多精彩内容