iOS-@property、@synthesize 、@dynamic的应用

 
@property的本质
 
@property = ivar(下划线实例变量) + getter/setter(存取方法);

 

 
在正规的 Objective-C 编码风格中,存取方法有着严格的命名规范。 正因为有了这种严格的命名规范,所以 Objective-C 这门语言才能根据名称自动创建出存取方法。
完成属性定义后,编译器会自动编写访问这些属性所需的方法,此过程叫做“自动合成”(autosynthesis)。需要强调的是,这个过程由编译 器在编译期执行,所以编辑器里看不到这些“合成方法”(synthesized method)的源代码。除了生成方法代码 getter、setter 之外,编译器还要自动向类中添加适当类型的实例变量,并且在属性名前面加下划线,以此作为实例变量的名字。
 
我们每次在增加一个属性,系统都会在 ivar_list 中添加一个成员变量的描述,在 method_list 中增加 setter 与 getter 方法的描述,在属性列表中增加一个属性的描述,然后计算该属性在对象中的偏移量,然后给出 setter 与 getter 方法对应的实现,在 setter 方法中从偏移量的位置开始赋值,在 getter 方法中从偏移量开始取值,为了能够读取正确字节数,系统对象偏移量的指针类型进行了类型强转.
 
@property有两个对应的词,一个是 @synthesize,一个是 @dynamic。如果 @synthesize和 @dynamic都没写,那么默认的就是@syntheszie var = _var; (自动合成的)
 

自动合成(@synthesize)

默认就是自动合成

动态合成(@dynamic)

既然有自动合成,那么相对应的就要有非自动合成,非自动合成又称为动态合成。
 
1. 什么是synthesize
synthesize中文意思是合成,代码中我们经常这样用。
1 @interface Test: NSObject
2 @property (nonatomic, unsafe_unretained) int i;
3 @end
4  
5 @implementation Test
6 @synthesize i = _i;
7 @end

 

使用synthesize的2个步骤:
  • 首先你要有在类声明中使用property声明的属性。
  • 第二在类实现中,写出 @synthesize + 变量名。

2. synthesize有什么作用?

平时在使用中,在类声明中添加了property后,根本不需要在实现中添加@synthesize。
因为OC为property属性声明添加了自动合成,也就是说系统自动帮你添加了@synthesize。
所以,synthesize是property属性的一部分,它是系统为property生成变量的重要步骤。
synthesize具体做了些什么呢?它只做2件事:
  • 生成成员变量,如上面的Test类就会生成一个名字为 _i 的 int型变量。
  • 为属性生成setter/getter方法,如上面的Test类会生成setI:和i两个方法。

3. synthesize什么情况下会用?

正常情况下,你不需要使用的synthesize,因为大多数情况下,系统都会为你自动合成。
但是,你必须能清楚,系统自动合成有时候也是会失效的。这时候就需要你手动添加 synthesize。
这些情况大约有3种:
  • 修改生成的成员变量名字。
  • 手动添加了 setter/getter 方法。
  • 实现了带有 property 属性的 protocol。

3.1 修改生成的成员变量名字

 1 @interface Test: NSObject
 2 @property (nonatomic, readonly, unsafe_unretained) int i;
 3 @end
 4  
 5 @implementation Test
 6  
 7 @synthesize i = shuaiI;
 8  
 9 //@synthesize i; //如果只写这个相当于 @synthesize i = i;
10  
11 -(void) print{
12     //NSLog(@" print test i = %d", _i); //这个_i好土,换个帅气的名字。
13     NSLog(@" print test i = %d", shuaiI); //帅气的名字就是shuaiI。
14 }
15 @end

 

虽然可以使用@synthesize关键字修改变量名,但是如无特殊需求,不建议这样做。因为默认情况下编译器已经为我们生成了变量名,大多数的项目、开发者也都会遵循这样的规范,既然苹果已经定义了一个好的规范,为什么不遵守呢?

3.2 手动添加了setter/getter方法

如果你的属性是只读属性,但是你重写了getter方法,系统不会为你自动生成成员变量。你需要添加@synthesize。
 1 @interface Test: NSObject
 2 @property (nonatomic, readonly, unsafe_unretained) int i;
 3 @end
 4  
 5 @implementation Test
 6 -(int)i{
 7     return _i;
 8 }
 9  
10 @synthesize i = _i;//不加这个会报错
11  
12 -(void) print{
13     NSLog(@" print test i = %d", _i);
14 }
15  
16 @end

 

如果你的属性可读可写,但是你同时重写了setter/getter方法,系统不会为你自动生成成员变量。你需要添加@synthesize。这种情况下,你如果只重写了setter/getter其中一个,系统仍然会执行自动合成。
 
 1 @interface Test: NSObject
 2 @property (nonatomic, unsafe_unretained) int i;
 3 @end
 4  
 5 @implementation Test
 6  
 7 -(int)i{
 8     return _i;
 9 }
10  
11 -(void) setI:(int)i{
12     _i = i;
13 }
14  
15 @synthesize i = _i;//不加这个会报错
16  
17 -(void) print{
18     NSLog(@" print test i = %d", _i);
19 }
20  
21 @end

 

3.3 实现了带有property属性的protocol

 1 @protocol TestProtocol<NSObject>
 2 @property (nonatomic, unsafe_unretained) int i;
 3 @end
 4  
 5 @interface Test: NSObject<TestProtocol>
 6 @end
 7  
 8 @implementation Test
 9  
10 @synthesize i = _i;//不加这个会报错
11  
12 -(void) print{
13     NSLog(@" print test i = %d", _i);
14 }
15  
16 @end

 

4. 其他不会自动合成的情况

还有些情况,系统不会为属性自动合成变量和setter/getter方法,但是你也不需要手动添加@synthesize。
这些情况有:
  • 使用了@dynamic。
  • 在Category中添加的property。
  • 子类覆盖了父类的同名属性。

4.1 什么是@dynamic

@dynamic 使用场合同 @synthesize,它的作用和@synthesize相反,它告诉系统,不要为property声明的属性添加成员变量。会有其他地方添加的。
所以用到@dynamic的地方很少,那么在什么情况下会使用到呢?
能想到的大概只有1种:动态生成类和变量的情景中。如动态model生成。

4.2 Category中声明property

Category中直接声明property是没有意义的,相当于没写。
所以你在Category中即使写了property,也要手动添加setter/getter方法。
property就是为了简写setter/getter方法,你都手动写了,也就没必要加property了。
但是你仍然可以写property。

4.3 子类覆盖父类同名属性

 1 @interface Super: NSObject
 2 @property (nonatomic, unsafe_unretained) int i;
 3 @end
 4  
 5 @implementation Super
 6 @end
 7  
 8 @interface Child: Super
 9 @property (nonatomic, unsafe_unretained) int i;
10 @end
11  
12 @implementation Child
13  
14 -(int)i{
15 return 1;
16 }
17  
18 +(void) test{
19 IMP superSetterMethod = class_getMethodImplementation(Super.class, @selector(setI:));
20 IMP childSetterMethod = class_getMethodImplementation(Child.class, @selector(setI:));
21 NSLog(@" setter is Equal = %d", superSetterMethod == childSetterMethod);
22 IMP superGetterMethod = class_getMethodImplementation(Super.class, @selector(i));
23 IMP childGetterMethod = class_getMethodImplementation(Child.class, @selector(i));
24 NSLog(@" getter is Equal = %d", superGetterMethod == childGetterMethod);
25 }
26 @end

 

上面代码写出来,xcode就会有提示,告诉你要为Child的i属性添加@synthesize。
而执行[Child test]的输出结果为:
1
0
复制代码
说明Child里虽然写了property,但是并未生成setter/getter方法。
另外需要注意的是,子类同名属性如果和父类的属性类型不同。则可能会崩溃。所以不要这样写,换个变量名字好了。
 
posted @ 2020-08-19 15:40  俊华的博客  阅读(663)  评论(0编辑  收藏  举报