@property 和 @synthesize 的剂量如何发挥作用?
可能的重复:
属性声明和自动后备存储分配
我知道了@的基本功能property 和 @synthesize 指令,但最近我发现了一个示例代码:
TTDownloader 中没有任何类成员变量,但它工作得很好。
TTDownloader *dl = [[TTDownloader alloc] init];
NSLog(@"%d %@", dl.aaa, dl.bbb); // result: 0 (null)
dl.aaa = 101;
dl.bbb = [NSNumber numberWithInt:201];
NSLog(@"%d %@", dl.aaa, dl.bbb); // result: 101 201
我的问题如下:
aaa 和 bbb 在哪里?我什至没有地方保存这些数据。 TTDownloader 是空的(除了一些从 NSObject 派生的变量和函数)。
我是否错过了 Objective C 的一些出色功能?它们在《Obj-C 编程指南》中有描述吗?我刚刚读了其中的一部分。
提前致谢。
Possible Duplicate:
Property Declaration and Automatic Backing Storage Allocation
I know the basic functions of @property and @synthesize directives, but recently I found a sample code:
There's not any class member variables in TTDownloader, but it works perfect.
TTDownloader *dl = [[TTDownloader alloc] init];
NSLog(@"%d %@", dl.aaa, dl.bbb); // result: 0 (null)
dl.aaa = 101;
dl.bbb = [NSNumber numberWithInt:201];
NSLog(@"%d %@", dl.aaa, dl.bbb); // result: 101 201
My questions are listed below:
Where is aaa and bbb? I don't even have a place to hold these data. TTDownloader is empty (except some variables and functions derived from NSObject).
Did I miss some great features of Objective C? Are they described in Obj-C Programming Guide, I just read part of it.
Thanks in advance.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
在除 32 位 OS X 之外的所有平台上的现代运行时中,当您合成属性时,如果尚未声明实例变量,则会自动创建它。所以
aaa 和 bbb 是属性,即每个都是一对合成的访问器方法,并分别使用实例变量 _aaa 和 _bbb。
是的,见上文。
In the modern runtime on all platforms except 32 bit OS X, when you synthesize a property, if the instance variable has not been declared, it will be created automatically. so
aaa and bbb are properties i.e. each is a pair of accessor methods which are synthesized and use the instance variables _aaa and _bbb respectively.
Yes, see above.