Objective-C 11 属性property
来源:互联网 发布:加强网络监督文化建设 编辑:程序博客网 时间:2024/05/18 03:01
一般成员变量对外都需要set和get方法。例如Student.h文件中
#import <Foundation/Foundation.h>@interface Student : NSObject { int age; int _no; float _height;}- (void)setAge:(int)newAge;- (int)age;- (void)setNo:(int)newNo;- (int)no;- (void)setHeight:(float)newHeight;- (float)height;
而且还需要在.m文件中实现这俩方法。
- (void)setAge:(int)newAge { age = newAge;}- (int)age { return age;}
成员变量少还好,但是成员变量多的话就需要写许多代码来实现这些方法。
这样会造成大量的垃圾代码,苹果公司在Objective-C 2.0中引入了属性(property),它组合了新的预编译指令和新的属性访问器语法。新的属性功能显著减少了必须编写的冗长代码的数量。
@property
@property会自动声明和实现变量的get和set方法。
Student.h文件
#import <Foundation/Foundation.h>@interface Student : NSObject { int age; int _no; float _height;}// 当编译器遇到@property时,会自动展开成getter和setter的声明@property int age;//- (void)setAge:(int)newAge;//- (int)age;@property int no;//- (void)setNo:(int)newNo;//- (int)no;@property float height;//- (void)setHeight:(float)newHeight;//- (float)height;@end通过@property可以省略setter和getter方法的生明。效果和上面的一样。
Student.m文件
#import "Student.h"@implementation Student// @synthesize age, height, no;// @synthesize会自动生成getter和setter的实现// @synthesize默认会去访问跟age同名的变量// 如果找不到同名的变量,会自动生成一个<span style="color:#ff0000;">私有</span>的同名变量age// @synthesize age;@synthesize age;//- (void)setAge:(int)newAge {// age = newAge;//}////- (int)age {// return age;//}@synthesize height = _height;//- (void)setHeight:(float)newHeight {// _height = newHeight;//}////- (float)height {// return _height;//}@synthesize no = _no;//- (void)setNo:(int)newNo {// _no = newNo;//}////- (int)no {// return _no;//}@end@synthesize也是一种新的编译器功能,表示“创建了该属性的访问代码”。
所有的属性都是基于变量的,所以在合成(synthesize)getter和setter方法的时候,编译器会自动创建与属性名称相同的实例变量,所以可以省略变量age的声明。但是如果变量想要自擂直接通过属性访问,则不能省略,必需在头文件中声明。
@synthesize height = _height; 代表getter和setter会去访问_height这个成员变量,没有这个变量会自动创建_height;
在Xcode4.5以后的版本中,可以不必使用@synthesize了,这时编译器自动创建属性名称前加下划线的实例变量,@property int age;@property int no;@property float height;@property int number;不使用@synthesize会访问_age _no _height _number; 如果找不到会自动创建私有变量int _age; int _no; float _height; int _number;
两个地方可以添加实例变量的声明:头文件和实现文件。然后有什么区别呢?
假设有一个子类,并且想要从子类直接通过属性访问父类中的变量,这种情况变量必须放在头文件中。如果变量只是属于当前类,则可以把他们放在.m文件中。
参数
属性设置参数的方法:@property(参数,参数)类型 名字;
其中参数可以设置也可以不设置,当然可以是1个或多个了。
读写属性(readwrite/readonly)
默认情况下属性是readwrite,可读可写的,也就是会生成setter和getter方法。
readonly 只读的,只生成getter方法,不生成setter方法。
atomicity(nonatomic)
默认情况是atomic,读取函数为原子操作。
atomic就代表给方法进行加锁,保证线程安全。
nonatomic代表方法不需要考虑线程安全问题。
setter语意(assign/retain/copy)
assign/retain/copy 决定了以何种方式对数据成员赋予新值。assign是默认的。
1 copy/reain/assign 在其中选择一个来确定属性的setter如何处理这个属性。NSObject对象采用这个中方式。
2 一些特别的Object比如NSSstring使用copy。
3 assign关键字代表setter直接赋值,而不是复制或者保留它。适用于基本数据类型,比如NSInteger和CGFloat,或者你并不直接拥有的类型,比如delegates。
4 retain代表:在set方法中,release旧值,retain新值
strong与weak
strong:强引用,也是我们通常说的引用,其存亡直接决定了所指向对象的存亡。如果不存在指向一个对象的引用,并且此对象不再显示在列表中,则此对象会被从内存中释放。
weak:弱引用,不决定对象的存亡。即使一个对象被持有无数个弱引用,只要没有强引用指向它,那么还是会被清除。 指针置为nil;一般storboard连接的控件都是weak。
strong与retain功能相似;weak与assign相似,只是当对象消失后weak会自动把指针变为nil;
- Objective-C 11 属性property
- Objective-C 学习笔记 11 - 属性Property
- Objective C 2.0 属性(Property)
- objective C property属性详解
- Objective C 2.0 属性(Property)
- Objective-C 2.0属性(Property)介绍
- objective-c中property的属性
- Objective-C中@property属性使用
- 【Objective-C】@property的所有属性详解
- iOS Objective-C @property属性的用法
- Objective-C之property属性分析
- Objective-c的@property,属性介绍
- Objective-C的属性(property)解析:
- Objective-c 中@property中属性详解
- 1、Objective-C的@property属性和@synthesize属性
- 1、Objective-C的@property属性和@synthesize属性
- Objective C 2.0 简明教程 (5) 属性(Property)
- iPhone SDK开发之 objective C property属性详解
- MySQL-SQL Server、Oracle中的索引维护方式
- Spark基础知识学习分享
- MySQL、SQLServer、Oracle中约束和索引的关系
- MySQL中的级联约束
- MySQL将查询结果输出都爱服务器文件中
- Objective-C 11 属性property
- 【SPOJ-SUBST1】New Distinct Substrings【后缀数组】
- applicationContext.xml配置说明
- 思维导图及绘制思维导图的软件
- 解决错误 ImportError: No system module 'pywintypes' (pywintypes27.dll)
- POJ 3320 Jessica's Reading Problem
- Java设计模式之二:抽象工厂AbstractFactory
- Mysql中组合的insert和update语句
- C语言字符串的常见特殊操作(除了string.c实现的那些接口)