@表明“Objective-C”的标志,证实您正在使用Objective-C语言 php
Objective-C语言关键词,@property与@synthesize配对使用。 html
功能:让编译好器自动编写一个与数据成员同名的方法声明来省去读写方法的声明。 多线程
如: app
一、在头文件中: 函数
@property int count;
等效于在头文件中声明2个方法: 性能
- (int)count; -(void)setCount:(int)newCount;
二、实现文件(.m)中 ui
@synthesize count;
等效于在实现文件(.m)中实现2个方法。 编码
- (int)count { return count; } -(void)setCount:(int)newCount { count = newCount; }
以上等效的函数部分由编译器自动帮开发者填充完成,简化了编码输入工做量。 atom
格式: spa
声明property的语法为:@property (参数1,参数2) 类型 名字;
如:
@property(nonatomic,retain) UIWindow *window;
其中参数主要分为三类:
读写属性: (readwrite/readonly)
setter语意:(assign/retain/copy)
原子性: (atomicity/nonatomic)
各参数意义以下:
readwrite: 产生setter\getter方法
readonly: 只产生简单的getter,没有setter。
assign: 默认类型,setter方法直接赋值,而不进行retain操做
retain: setter方法对参数进行release旧值,再retain新值。
copy: setter方法进行Copy操做,与retain同样
nonatomic: 禁止多线程,变量保护,提升性能
参数类型
参数中比较复杂的是retain和copy,具体分析以下:
getter 分析
一、
@property(nonatomic,retain)test* thetest; @property(nonatomic ,copy)test* thetest;
等效代码:
-(void)thetest { return thetest; }
二、
@property(retain)test* thetest; @property(copy)test* thetest;
等效代码:
-(void)thetest { [thetest retain]; return [thetest autorelease]; }
setter分析
一、
@property(nonatomic,retain)test* thetest; @property(retain)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest { if (thetest!= newThetest) { [thetestrelease]; thetest= [newThetest retain]; } }
二、
@property(nonatomic,copy)test* thetest; @property(copy)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest { if (thetest!= newThetest) { [thetest release]; thetest= [newThetest copy]; } }
nonatomic
若是使用多线程,有时会出现两个线程互相等待对方致使锁死的状况(具体能够搜下线程方面的注意事项去了解)。在没有(nonatomic)的状况下,即默认(atomic),会防止这种线程互斥出现,可是会消耗必定的资源。因此若是不是多线程的程序,打上(nonatomic)便可
retain
代码说明
若是只是@property NSString*str; 则经过@synthesize自动生成的setter代码为:
-(void)setStr:(NSString*)value{ str=value; }
若是是@property(retain)NSString*str; 则自动的setter内容为:
-(void)setStr:(NSString*)v{ if(v!=str){ [str release]; str=[v retain]; } }
全部者属性
咱们先来看看与全部权有关系的属性,关键字间的对应关系。
strong | __strong | 有 |
weak | __weak | 无 |
unsafe_unretained | __unsafe_unretained | 无 |
copy | __strong | 有 |
assign | __unsafe_unretained | 无 |
retain | __strong | 有 |
该属性值对应 __strong 关键字,即该属性所声明的变量将成为对象的持有者。
weak该属性对应 __weak 关键字,与 __weak 定义的变量一致,该属性所声明的变量将没有对象的全部权,而且当对象被破弃以后,对象将被自动赋值nil。
而且,delegate 和 Outlet 应该用 weak 属性来声明。同时,如上一回介绍的 iOS 5 以前的版本是没有 __weak 关键字的,因此 weak 属性是不能使用的。这种状况咱们使用 unsafe_unretained。
unsafe_unretained等效于__unsafe_unretaind关键字声明的变量;像上面说明的,iOS 5以前的系统用该属性代替 weak 来使用。
copy与 strong 的区别是声明变量是拷贝对象的持有者。
assign通常Scalar Varible用该属性声明,好比,int, BOOL。
retain该属性与 strong 一致;只是可读性更强一些。
参考:
http://blog.eddie.com.tw/2010/12/08/property-and-synthesize/
http://www.cocoachina.com/bbs/read.php?tid=7322
http://www.cnblogs.com/pinping/archive/2011/08/03/2126150.html
在 Objective-C官方文档 中的Property一章里有对类Property详细说明。
@property中的声明列表已分类为如下几种:
1, 声明属性的访问方法:
2,声明属性写操做权限:
3,声明写方法的实现:
-(void)setName:(NSString*)_name{ name = _name; }
-(void)setName:(NSString*)_name{ //首先判断是否与旧对象一致,若是不一致进行赋值。 //由于若是是一个对象的话,进行if内的代码会形成一个极端的状况:当此name的retain为1时,使这次的set操做让实例name提早释放,而达不到赋值目的。 if ( name != _name){ [name release]; name = [_name retain]; } }
4,访问方法的原子性:
[ _internal lock ]; // lock using an object-level lock id result = [ [ value retain ] autorelease ]; [ _internal unlock ]; return result;
但若是设置nonatomic时,属性的访问为非原子性访问。
来源:http://wiki.magiche.net/pages/viewpage.action?pageId=1540101
@synthesize tabBarController=_tabBarController;
@synthesize 中能够定义 与变量名不相同的getter和setter的命名,籍此来保护变量不会被不恰当的访问