声明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 分析
1、 @property(nonatomic,retain)test* thetest;
@property(nonatomic ,copy)test* thetest;
等效代码:
-(void)thetest
{
return thetest;
}
2、@property(retain)test* thetest;
@property(copy)test* thetest;
等效代码:
-(void)thetest
{
[thetest retain];
return [thetest autorelease];
}
setter分析
1、 @property(nonatomic,retain)test* thetest;
@property(retain)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest {
if (thetest!= newThetest) {
[thetestrelease];
thetest= [newThetest retain];
}
}
2、@property(nonatomic,copy)test* thetest;
@property(copy)test* thetest;
等效于:
-(void)setThetest:(test *)newThetest {
if (thetest!= newThetest) {
[thetestrelease];
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];
}
}
分享到:
相关推荐
相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459
1 获取XML的数据DATA 2 调用解析类,提供对象名。 3 返回NSMutableDictionary或者NSMutableArray,键值封装。...@synthesize currentResult; @synthesize map; @synthesize list; ********0..........
@synthesize objectName; @synthesize lvUp; @synthesize root; @synthesize buildEnd; -(NSData *)objctPackage:(NSMutableDictionary *)object objectName:(NSString *)name xmlTemplateName:(NSString *)...
1. 使用@property和@synthesize声明一个成员变量,给其赋值是时要在前面加上"self.",以便调用成员变量的setmember方法。 直接调用成员变量并且给其赋值:member=[NSString stringWithFormat:@””];将不执行...
用Xcode4.5编写,突出新的性能。如:@synthesize by default(属性自动绑定)等。
自动创建简单的方法来获取和设置 JavaScript 模块的属性,类似于 iOS Objective-C 中的 @synthesize 指令。 用法 在这个例子中,我们在 MyClass 原型上创建了一个名为 name() 的 getter/setter 方法: var MyClass...
本套PPT,拥有以下OC语言的内容: ...二、OC的特有语法(点语法、@property、@synthesize关键字、id、构造方法、分类、description、SEL) 三、内存管理 四、协议(protocol)代码块(block) 五、ARC 等等
变量处理程序一个ObjC助手来使用varidict处理程序。 一行代码值得一千个单词...@implementation Consumer@synthesize handler = _handler;- ( void ) setHandler : (NDHandler) handler { static Validator validator
AppDelegate.m @synthesize bridge;...- ( BOOL )application:(UIApplication *)application didFinishLaunchingWithOptions:( NSDictionary *)launchOptions{ ... configure the jsCodeLocation here...
针对iOS开发中属性 property 和 synthesize 进行了详细介绍
先来看看效果图 实现方法 .h文件 @property (nonatomic, retain) NSArray *changeArray; @property (nonatomic, retain)...@synthesize changeArray = _changeArray; @synthesize changeString = _changeString; @sy
iOS项目引导您如何设置iOS项目?...代码质量和警告警告是由编译器团队添加的原因,例如,我从Weverything开始并禁用一些警告: Wno-objc-missing-property-synthesis-不想对属性进行@synthesize Wno未使用
资源分类:Python库 所属语言:Python 资源全名:synthesize-0.0.1-py3-none-any.whl 资源来源:官方 安装方法:https://lanzao.blog.csdn.net/article/details/101784059
3D-Synthesize3DviaDepthOrSil.zip,[CVPR 2017]通过建模多视图深度图或轮廓生成和重建三维形状,3D建模使用专门的软件来创建物理对象的数字模型。它是3D计算机图形的一个方面,用于视频游戏,3D打印和VR,以及其他...
Synthesize Synthesize is an android library which can create layout images in background threads, services, etc without inflating them in activity or fragments. Synthesize Documentation
Synthesize & Power Analyze
Code to synthesize Arithmetic Logic Unit
Synthesize3DviaDepthOrSil:[CVPR 2017]通过对多视图深度图或轮廓建模来生成和重建3D形状
synthesize中的install脚本旨在使将Graphite和相关服务轻松安装到现代Linux发行版上非常容易。 Synthesize构建为可在Ubuntu 18.04 LTS上运行。 它不会在其他Ubuntu版本或Linux发行版上运行。 该项目的目标不是成为...