main.m
1 //
2 // main.m
3 // @property参数与setter方法.m
4 //
5 // Created by sixleaves on 15/5/8.
6 // Copyright (c) 2015年 itcast. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10 #import "Person.h"
11 int main(int argc, const char * argv[]) {
12
13 Person *p = [[Person alloc] init];
14
15 p.rich = YES;
16
17 BOOL b = [p isRich];
18 NSLog(@"isRich = %d", b);
19
20 p.weight = 100;
21 NSLog(@"weight = % d", p.weight);
22 return 0;
23 }
24 /*
25 @property的相关参数详细介绍
26
27 1.与setter内存管理相关参数。
28 retain : release旧值,retain新值。也就是于我们自己实现setter方法内存管理一样。
29 assign : 直接赋值
30 copy : release旧值,copy新值
31
32 2.是否生成setter方法
33 readwrite:setter与getter都生成。
34 readonly:只生成getter
35
36 3.多线程管理
37 nonatomic:不保持原子性,性能高、
38 atomic:保持原子性,性能低(默认)
39
40 4.setter与getter的名字
41 setter :决定set方法的名称, 一定要用冒号:
42 getter :决定get方法的名称(一般用在BOOL类型上)
43
44 */
45
Person.h
1 //
2 // Person.h
3 // @property参数与setter方法.m
4 //
5 // Created by sixleaves on 15/5/8.
6 // Copyright (c) 2015年 itcast. All rights reserved.
7 //
8
9 #import <Foundation/Foundation.h>
10
11 @interface Person : NSObject
12
13 @property (nonatomic, assign, getter = isRich) BOOL rich;
14
15 @property (nonatomic, retain) NSString *name;
16
17 @property (nonatomic, assign, readwrite, getter = abc, setter = setAbc:) int weight;
18
19 @end
20
Person.m
1 //
2 // Person.m
3 // @property参数与setter方法.m
4 //
5 // Created by sixleaves on 15/5/8.
6 // Copyright (c) 2015年 itcast. All rights reserved.
7 //
8
9 #import "Person.h"
10
11 @implementation Person
12
13 @end
14