转载自-http://blog.sina.com.cn/s/blog_71715bf8010166uj.html
1、简介:
property是Objective-C的关键词,与@synthesize配对使用,用来让编译好器自动生成与数据成员同名的方法声明。@synthesize则是用来生成对应声明方法的实现。
1.1 property的语法格式:
@property
这里的参数,主要有以下三种:
setter/getter方法(assign/retain/copy)
读写属性(readwrite/readonly)
atomicity(nonatomic)
1.2 三种方式的使用
assign/retain/copy
readonly关键字代表setter不会被生成,
atomicity的默认值是atomic,读取函数为原子操作。
1.2.1 copy/reain/assign
1.2.2 一些特别的Object比如NSSstring使用copy。
1.2.3 assign关键字代表setter直接赋值,而不是复制或者保留它。适用于基本数据类型,比如NSInteger和CGFloat,或者你并不直接拥有的类型,比如delegates。
2、如何使用property
1.1 没有property和有property的对比
在头文件定义 obj。在.m文件中使用
- #import
<UIKit/UIKit.h> -
- @interface
ViewController : UIViewController - {
-
NSObject *obj; - }
- @end
- -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
self.obj = nil;、 - }
加上property
- #import
<UIKit/UIKit.h> -
- @interface
ViewController : UIViewController - {
-
NSObject *obj; - }
- @property
(nonatomic,retain) NSObject *obj; - @end
那就是我们没事实现setter方法。
用@synthesize关键字实现getter 和setter。
在.m文件中
- @implementation
ViewController - @synthesize
obj; - -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
self.obj = nil; - }
3、@property和@synthesize关键字 生成的代码
到底@property和@synthesize关键字生成了什么代码呢?我们自己实现getter 和setter也可以替代这些关键字。
把这两个关键字对应的代码注释掉
.h
- #import
<UIKit/UIKit.h> -
- @interface
ViewController : UIViewController - {
-
NSObject *obj; - }
- //@property
(nonatomic,retain) NSObject *obj; - -(NSObject*)obj;
- -(void)setObj:(NSObject*)newObj;
- @end
.m
- @implementation
ViewController - //@synthesize
obj; - -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
self.obj = nil; - }
-
- -(NSObject*)obj{
-
return obj; - }
- -(void)setObj:(NSObject*)newObj{
-
if(obj != newObj){ -
[obj release]; -
obj = [newObj retain]; -
} - }
4、使用三种参数的对比
@property
@property
readwrite是默认行为,所以这两行代码等价
@property
@property
@property(atomic,assign)int
@property(atomic)
@property
对int 来说,atomic assign都是默认行为,所以这三行是等价的。
@property
只有int 等基础数据类型能这么写。对象必须加上赋值的类型。
@property
5、retain和copy实验。
使用copy。
.h文件
- #import
<UIKit/UIKit.h> -
- @interface
ViewController : UIViewController - {
-
NSString *string; - }
- @property
(nonatomic, copy) NSString *string; - @end
- @synthesize
string; - -
(void)viewDidLoad - {
-
[super viewDidLoad]; -
-
NSString *str = [[NSString alloc] initWithFormat:@"abcd"]; -
NSLog(@"str_Point:%p %@ retainCount:%d", str, str, [str retainCount]); -
self.string = str; -
NSLog(@"string_Point:%p %@ retainCount:%d", string, string, [string retainCount]); - }
2012-07-19 20:41:44.853 TestProject1[1213:f803] str_Point:0x6a8e0b0
2012-07-19 20:41:44.854 TestProject1[1213:f803] string_Point:0x6a8e0b0
内存地址是一样的,不是想其他文字所写的那样,拷贝了一份内存,这里用copy也是浅拷贝。retain也+1
使用retain
- #import
<UIKit/UIKit.h> -
- @interface
ViewController : UIViewController - {
-
NSString *string; - }
- @property
(nonatomic, retain) NSString *string; - @end
2012-07-19 20:42:08.113 TestProject1[1230:f803] str_Point:0x6d3b8f0
2012-07-19 20:42:08.114 TestProject1[1230:f803] string_Point:0x6d3b8f0
结果和上面copy一样。
注意:在IOS5之后,加入了Automatic Reference Counting (ARC),iOS5中新加了关键字有strong,