iOS中的@synthesize

常见的有两种用法 其实是一个意思

  • @synthesize student; 等价于 @synthesize student = student

  • @synthesize student = _student

ARC下 通过@property声明的属性,编译器会自动声明对应的成员变量和getter/setter方法 而@synthesize student = _student就是程序猿自己显式地声明属性对应的成员变量,而不用编译器自生成的

我们知道getter和setter方法里面操作的其实就是属性对应的成员变量 成员变量其实本质上就是一个变量指针

那么调用两者有什么区别呢?

1
2
self.name = @"我信你个鬼";
_name = @"我信你个大头鬼";

self.name本质上是调用的setter方法setName() 而setter方法是依赖于@property的属性修饰符的,比如retain,assign等属性。也就是会影响其引用计数

而_name = @”我信你个大头鬼” 只是变量的赋值 不涉及retain、assign等