关于@property和@synthesize的问题
通常我们有这样的代码
@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@synthesize window;
还有第二个版本
@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *_window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@synthesize window = _window;
问题是
- 最大的区别是什么?哪个更好?为什么?
- 对于第一个版本,为什么window的默认属性成员是什么呢,在第二个版本中是_window。 KVC 或 KVO 在内部工作吗?
Usually we have codes like these
@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@synthesize window;
And also second version
@interface TestAppDelegate : NSObject <UIApplicationDelegate> {
UIWindow *_window;
}
@property (nonatomic, retain) IBOutlet UIWindow *window;
@synthesize window = _window;
Quetions are
- what is the big difference ? Which better ? and Why ?
- For first version, Why what is the default attribute member of window ,which is _window in second version. Does that KVC or KVO work inside ?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
相同
在默认实例中,它与实际上不需要综合分配的情况 。并且可能会产生错误。
这是我这样做的正常方式。
我显式声明了实例变量
,以便我知道它在那里(它们通常彼此相邻。)
并且我将它们按保留的和不保留的分开。这样我就知道在我的 dealloc 方法中释放它们
in the default instance it is the same as
which would actually not need an Assignment on the synthesize. and would probably generate an error.
This is the normal way that I do it.
I explicitly declare the Instance Variable
so that I know it is there, (they are generally all right next to each other.)
and I separate them by the ones I retain and the ones I dont. So that I know to release them in my dealloc method