@property和@synthesize
来源:互联网 发布:状态机编程 编辑:程序博客网 时间:2024/05/01 23:33
从Objective-C2.0开始,可自动生成设值方法和取值方法(统称为存取方法)。具体步骤如下:
1)在接口部分中使用@property指令标识属性。
@interface Fraction : NSObject
@property int numerator, denominator;
@end
2)在实现部分使用@synthesize指令即可。
@inplementation Fraction
@synthesize numerator, denominator;
@end
如果使用了@property指令,就不需要在实现部分声明相应的实例变量。
通常,如果有称为x的属性,那么在实现部分包括以下行会导致编译器自动实现一个取值方法x和一个设置方法setX:
@synthesize x;
因为生成的存取方法是高效的,并且在使用多个核心的多台机器上,使用多线程时也可正常运行。(这里的说法呢,就是说线程安全的)
访问属性:
可以使用点运算符.,或发送消息[]
[myFraction numerator];
myFraction.numerator;
一般格式:
instance.property = value;
等价于
[instance setProperty: value]
需要指出的是,也可以对自定义的方法使用点运算符,不仅仅是使用在synthesize上(即属性)。
注意:
点运算符和发消息都是可以的,但是,点运算符通常使用在属性上,用于设置或取得实例变量的值。方法在Apple的文档中被标记为任务(Task),任务通常不是由点运算符执行的,而是使用传统的方括号形式的消息表达式作为首选的语法。
另外,使用合成(synthesize)的存取方法,属性名称的前面不要以new、alloc、copy和init这些此开头。这与编译器的一些假定有关,因为编译器会合成相应的方法。
- @property和@synthesize浅解
- @property和@synthesize见解
- property和synthesize
- @property和@synthesize
- @synthesize和@property详解
- C15_OC05-@property和@synthesize
- @property和@synthesize
- property 和 synthesize解说
- @property和@synthesize
- iOS_@property和@synthesize
- @property和@synthesize
- 08-property和synthesize
- @property和@synthesize
- @property 和 @synthesize
- oc_study16 @property和@synthesize
- 关键字@property和@synthesize
- iOS@synthesize 和 @property
- @property和@synthesize
- Mysql数据库的mysql Schema 到底有哪些东西& 手工注入的基础要领
- PHP在GB2312编码下截断字符串
- android笔记
- poj 3125 Printer Queue (队列)
- Luajit加密lua
- @property和@synthesize
- Android Activity 生命周期
- https+xml服务端/客户端java后台编写及soapUI https测试
- AVR控制继电器
- 容斥原理+质因数分解+组合数学
- 《Tips for Optimizing C/C++ Code》译文
- 设计模式入门之职责链模式Chain Of Responsibility
- ZooKeeper 会话超时
- OpenNI 1.5.4.0下VS2010开发环境的配置