iOS中你可能没有完全弄清楚的(一)synthesize

5,927 阅读4分钟

1. 什么是synthesize

synthesize中文意思是合成,代码中我们经常这样用。

@interface Test: NSObject
@property (nonatomic, unsafe_unretained) int i;
@end

@implementation Test
@synthesize i;
@end

使用synthesize的2个步骤:

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

2. synthesize有什么作用?

平时在使用中,在类声明中添加了property后,根本不需要在实现中添加@synthesize

因为OCproperty属性声明添加了自动合成,也就是说系统自动帮你添加了@synthesize

所以,synthesizeproperty属性的一部分,它是系统为property生成变量的重要步骤。

synthesize具体做了些什么呢?它只做2件事:

  • 生成成员变量,如上面的Test类就会生成一个名字为 _iint型变量。
  • 为属性生成setter/getter方法,如上面的Test类会生成setI:i两个方法。

3. synthesize什么情况下会用?

正常情况下,你不需要使用的synthesize,因为大多数情况下,系统都会为你自动合成。

但是,你必须能清楚,系统自动合成有时候也是会失效的。这时候就需要你手动添加 synthesize

这些情况大约有3种:

  • 修改生成的成员变量名字。
  • 手动添加了 setter/getter 方法。
  • 实现了带有 property 属性的 protocol

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

@interface Test: NSObject
@property (nonatomic, readonly, unsafe_unretained) int i;
@end

@implementation Test

@synthesize i = shuaiI;

//@synthesize i; //如果只写这个相当于 @synthesize i = i;

-(void) print{
	//NSLog(@" print test i = %d", _i); //这个_i好土,换个帅气的名字。
	NSLog(@" print test i = %d", shuaiI); //帅气的名字就是shuaiI。
}
@end

3.2 手动添加了setter/getter方法

如果你的属性是只读属性,但是你重写了getter方法,系统不会为你自动生成成员变量。你需要添加@synthesize

@interface Test: NSObject
@property (nonatomic, readonly, unsafe_unretained) int i;
@end

@implementation Test
-(int)i{
	return _i;
}

@synthesize i = _i;//不加这个会报错

-(void) print{
	NSLog(@" print test i = %d", _i);
}

@end

如果你的属性可读可写,但是你同时重写了setter/getter方法,系统不会为你自动生成成员变量。你需要添加@synthesize。这种情况下,你如果只重写了setter/getter其中一个,系统仍然会执行自动合成。

@interface Test: NSObject
@property (nonatomic, unsafe_unretained) int i;
@end

@implementation Test

-(int)i{
	return _i;
}

-(void) setI:(int)i{
	_i = i;
}

@synthesize i = _i;//不加这个会报错

-(void) print{
	NSLog(@" print test i = %d", _i);
}

@end

3.3 实现了带有property属性的protocol

@protocol TestProtocol<NSObject>
@property (nonatomic, unsafe_unretained) int i;
@end

@interface Test: NSObject<TestProtocol>
@end

@implementation Test

@synthesize i = _i;//不加这个会报错

-(void) print{
	NSLog(@" print test i = %d", _i);
}

@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 子类覆盖父类同名属性

@interface Super: NSObject
@property (nonatomic, unsafe_unretained) int i;
@end

@implementation Super
@end

@interface Child: Super
@property (nonatomic, unsafe_unretained) int i;
@end

@implementation Child

-(int)i{
    return 1;
}

+(void) test{
    IMP superSetterMethod = class_getMethodImplementation(Super.class, @selector(setI:));
    IMP childSetterMethod = class_getMethodImplementation(Child.class, @selector(setI:));
    NSLog(@" setter is Equal = %d", superSetterMethod == childSetterMethod);
    
    IMP superGetterMethod = class_getMethodImplementation(Super.class, @selector(i));
    IMP childGetterMethod = class_getMethodImplementation(Child.class, @selector(i));
    NSLog(@" getter is Equal = %d", superGetterMethod == childGetterMethod);
}
@end

上面代码写出来,xcode就会有提示,告诉你要为Child的i属性添加@synthesize。

而执行[Child test]的输出结果为:

1
0

说明Child里虽然写了property,但是并未生成setter/getter方法。

另外需要注意的是,子类同名属性如果和父类的属性类型不同。则可能会崩溃。所以不要这样写,换个变量名字好了。

(完)