`
yidongkaifa
  • 浏览: 4052562 次
文章分类
社区版块
存档分类
最新评论

Objective-C语法property详解

 
阅读更多

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直接赋值,而不是复制或者保留它。适用于基本数据类型,比如NSIntegerCGFloat,或者你并不直接拥有的类型,比如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
编译能通过,运行,崩溃,提示错误reason: '-[ViewController setObj:]: unrecognized selector sent to instance 0x6b6c480

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

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

在.m文件中

@implementation ViewController
@synthesize obj;
- (void)viewDidLoad
{
    [super viewDidLoad];
    self.obj = nil;
}
运行,程序运行正常。说明setter 起作用了。

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];
    }
}
再运行,也能正常启动。说明自己写的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、retaincopy实验。

使用copy。

.h文件

#import <UIKit/UIKit.h>

@interface ViewController : UIViewController
{
    NSString *string;
}
@property  (nonatomic, copy) NSString *string;
@end


.m文件

@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

#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/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

著作权声明:本文由http://blog.csdn.net/totogo2010/原创,欢迎转载分享。请尊重作者劳动,转载时保留该声明和作者博客链接,谢谢!

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics