iOS Xcode 4 属性访问
我最近切换到 Xcode 4,但我不太理解这种编写访问器的新方法。 例如,在创建新项目时自动生成的应用程序委托类中,window
对象没有在@interface
中声明,而是这样
@property (nonatomic, retain) IBOutlet UIWindow *window;
:在实现文件中,我们有@synthesize window=_window;
。 在函数中,我们有 self.window
或 _window
。
例如:
[self.window makeKeyAndVisible]; // in didFinishLaunchingWithOptions function
[_window release]; // in dealloc function
你能解释一下其中的区别吗?为什么@interface
中没有任何内容,为什么我们使用@synthesize window=_window;
而不是@synthesize window ;
以及 self.window
和 _window
之间有什么区别,我的意思是我什么时候必须多调用一个而不是另一个?
我有点迷失了,感觉就像我所做的新代码试图做同样的事情但无法正常工作......
谢谢!
I switched to Xcode 4 recently and I don't really understand this new way to write accessors.
For example, in the application delegate class that is auto-generated when creating a new project, the window
object is not declared in the @interface
but just this way:
@property (nonatomic, retain) IBOutlet UIWindow *window;
Then, in the implementation file, we have the @synthesize window=_window;
.
And in the functions, we have either self.window
OR _window
.
For example:
[self.window makeKeyAndVisible]; // in didFinishLaunchingWithOptions function
[_window release]; // in dealloc function
Can you explain me the difference, why there is nothing in the @interface
, why we do @synthesize window=_window;
instead of @synthesize window;
and what is the difference between self.window
and _window
, I mean when do I have to call one more than the other?
I'm a bit lost, and feel like the new code I doing trying to do the same in not working properly...
Thanks!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
“为什么
@interface
中没有任何内容”运行时正在为您合成 ivar。
“我们为什么要这样做
@synthesize window=_window;
这意味着
window
属性将使用名为_window
的ivar(默认情况下ivar名称是属性的名称)“
self.window
和_window
之间有什么区别?”前者使用
window
“getter”方法(即foo = [self window]
),后者直接访问ivar。“为什么我必须多调用一个而不是另一个?”
通常认为在
dealloc
方法中使用访问器方法是不安全的,这意味着首选使用 ivar。"Why is there nothing in the
@interface
"The runtime is synthesizing the ivar for you.
"Why do we do
@synthesize window=_window;
This means that the
window
property will use an ivar named_window
(by default the ivar name is the name of the property)"What is the difference between
self.window
and_window
?"The former is using the
window
"getter" method (ie,foo = [self window]
), and the latter is accessing the ivar directly."Why do I have to call one more than the other?"
It is generally considered unsafe to use accessor methods in your
dealloc
method, which means using the ivar is preferred.这与 Xcode 4 无关。这是 Objective-C 2.0(Xcode 4 在创建项目模板时默认使用它)。
我建议阅读 The Objective 中有关属性的章节-C 编程语言,这应该使事情变得更加清晰
并且“旧方式”做事仍然有效。您不必在一夜之间更改所有内容,如果您不喜欢自动创建的代码,只需删除它即可,直到您对新语法感到满意为止。
This has nothing to do with Xcode 4. This is Objective-C 2.0 (which Xcode 4 uses by default when creating project templates).
I recommend reading the chapter on properties in The Objective-C Programming Language, that should make things much clearer
And doing things "the old way" will still work. You don't have to change everything overnight, simply remove the auto-created code if you don't like it, until you feel comfortable with the new syntax.