您的位置:首页 > 移动开发 > Objective-C

【转】Objective-C语法property详解

2015-09-11 11:07 417 查看

1、简介:

property是Objective-C的关键词,与@synthesize配对使用,用来让编译好器自动生成与数据成员同名的方法声明。@synthesize则是用来生成对应声明方法的实现。

1.1 property的语法格式:

@property (参数1,参数2)类型名字;

这里的参数,主要有以下三种:

setter/getter方法(assign/retain/copy)

读写属性(readwrite/readonly)

atomicity(nonatomic)

1.2 三种方式的使用

assign/retain/copy 代表赋值的方式。

readonly关键字代表setter不会被生成, 所以它不可以和 copy/retain/assign组合使用。

atomicity的默认值是atomic,读取函数为原子操作。

1.2.1 copy/reain/assign 在其中选择一个来确定属性的setter如何处理这个属性。NSObject对象采用这个中方式。

1.2.2 一些特别的Object比如NSSstring使用copy。

1.2.3 assign关键字代表setter直接赋值,而不是复制或者保留它。适用于基本数据类型,比如NSInteger和CGFloat,或者你并不直接拥有的类型,比如delegates。

2、如何使用property

1.1 没有property和有property的对比

在头文件定义 obj。在.m文件中使用

[cpp] view plaincopy

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{

NSObject *obj;

}

@end

[cpp] view plaincopy

- (void)viewDidLoad

{

[super viewDidLoad];

self.obj = nil;、

}

提示不可用。



加上property

[cpp] view plaincopy

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{

NSObject *obj;

}

@property (nonatomic,retain) NSObject *obj;

@end

编译能通过,运行,崩溃,提示错误 reason: '-[ViewController setObj:]: unrecognized selector sent to instance 0x6b6c480

那就是我们没事实现setter方法。

用@synthesize关键字实现getter 和setter。

在.m文件中

[cpp] view plaincopy

@implementation ViewController

@synthesize obj;

- (void)viewDidLoad

{

[super viewDidLoad];

self.obj = nil;

}

运行,程序运行正常。说明setter 起作用了。

3、@property和@synthesize关键字 生成的代码

到底@property和@synthesize关键字生成了什么代码呢?我们自己实现getter 和setter也可以替代这些关键字。

把这两个关键字对应的代码注释掉

.h

[cpp] view plaincopy

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{

NSObject *obj;

}

//@property (nonatomic,retain) NSObject *obj;

-(NSObject*)obj;

-(void)setObj:(NSObject*)newObj;

@end

.m

[cpp] view plaincopy

@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];

}

}

再运行,也能正常启动。说明自己写的getter 和setter替代了property。

4、使用三种参数的对比

@property (nonatomic,retain)NSObject *obj;

@property (nonatomic,retain,readwrite) NSObject *obj;

readwrite是默认行为,所以这两行代码等价

@property (retain) NSObject *obj;

@property (atomic,retain) NSObject *obj;

atomic是默认行为,所以这两行代码是等价的。

@property(atomic,assign)int number;

@property(atomic) int number;

@property int number;

对int 来说,atomic assign都是默认行为,所以这三行是等价的。

@property NSObject *obj;这样写行吗?不行的,报警告



只有int 等基础数据类型能这么写。对象必须加上赋值的类型。

@property (retain) NSObject *obj;这样就没问题了。何时使用assign、何时使用retain、copy后面再讲。

5、retain和copy实验。

使用copy。

.h文件

[cpp] view plaincopy

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController

{

NSString *string;

}

@property (nonatomic, copy) NSString *string;

@end

.m文件

[cpp] view plaincopy

@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 abcd retainCount:1

2012-07-19 20:41:44.854 TestProject1[1213:f803] string_Point:0x6a8e0b0 abcd retainCount:2

内存地址是一样的,不是想其他文字所写的那样,拷贝了一份内存,这里用copy也是浅拷贝。retain也+1

使用retain

[cpp] view plaincopy

#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 abcd retainCount:1

2012-07-19 20:42:08.114 TestProject1[1230:f803] string_Point:0x6d3b8f0 abcd retainCount:2

结果和上面copy一样。

注意:在IOS5之后,加入了Automatic Reference Counting (ARC),iOS5中新加了关键字有strong, weak, unsafe_unretained。

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: