Objective-C语言关键词,与@synthesize配对使用。 功能:让编译好器自动编写一个与数据成员同名的方法声明来省去读写方法的声明。 如: 1、在头文件中: @property int count; 等效于在头文件中声明2个方法: - (int)count; -(void)setCount:(int)newCount; 2、实现文件(.m)中 @synthesize count; 等效于在实现文件(.m)中实现2个方法。 - (int)count { return count; } -(void)setCount:(int)newCount { count = newCount; }
以上等效的函数部分由编译器自动帮开发者填充完成,简化了编码输入工作量。
格式
声明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]; }
}
分享到:
相关推荐
相信每个初学者对@property和@synthesize都感到非常的陌生,在此给大家分享下我的自己的理解,有不当之处,还望多多指教。详细说明文章在下面连接http://blog.csdn.net/comeontom/article/details/7455459
@property作用: python的@property是python的一种装饰器,是用来修饰方法的。 我们可以使用@property装饰器来创建只读属性,@property装饰器会将方法转换为相同名称的只读属性,可以与所定义的属性配合使用,这样...
主要介绍了Python 中@property的用法详解,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友们下面随着小编来一起学习学习吧
主要介绍了IOS开发之@property的详细介绍的相关资料,希望通过本文能帮助到大家,大家理解并会使用,需要的朋友可以参考下
-130,19DDiA@CA@figuCaEiA@ API详解一ef花花的猪的N栏一ef花花的猪的N栏pS在成长@property(readonly, copy
主要介绍了Android 自定义gradle property详解及实例代码的相关资料,需要的朋友可以参考下
针对iOS开发中属性 property 和 synthesize 进行了详细介绍
Python的property属性的功能是:property属性内部进行一系列的逻辑计算,最终将计算结果返回。 使用property修饰的实例方法被调用时,可以把它当做实例属性一样 property的用法1——装饰器方式 在类的实例方法上...
主要为大家详细介绍了python3中的property使用方法,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
主要给大家介绍了关于Spring boot中PropertySource注解的使用方法,文中通过示例代码介绍的非常详细,对大家学习或者使用Spring boot具有一定的参考学习价值,需要的朋友们下面来一起学习学习吧。
主要介绍了Python 从attribute到property详解,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧
上一篇:Python 类与对象编程详解二(成员保护和访问限制) 目录类属性属性的定义操作类属性的三种方法属性存在的意义操作类属性的方法使用@property装饰器操作类属性通过@property装饰器对属性的取值和赋值加以...
PropertyGrid是一个很强大的控件,使用该控件做属性设置面板的一个好处就是你只需要专注于代码而无需关注UI的呈现,PropertyGrid会默认根据变量类型选择合适的控件显示。但是这也带来了一个问题,就是控件的使用变得...
博客实例代码:http://blog.csdn.net/xiaohao0724/article/details/54863112
JavaScript中对象的property有三个属性:1.writable。该property是否可写。2.enumerable。当使用for/in语句时,该property是否会被枚举。3.configurable。该property的属性是否可以修改,property是否可以删除。 在...
@property (strong, nonatomic)NSThread *thread1; @property (strong, nonatomic)NSThread *thread2; @property (strong, nonatomic)NSThread *thread3; @property (assign, nonatomic)int leftTickets; @end 实现...
博客示例代码:http://blog.csdn.net/xiaohao0724/article/details/54863112