iOS Xcode 4 属性访问

发布于 2024-11-09 11:00:50 字数 763 浏览 0 评论 0原文

我最近切换到 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

濫情▎り 2024-11-16 11:00:50
  1. “为什么@interface中没有任何内容”

    运行时正在为您合成 ivar。

  2. “我们为什么要这样做@synthesize window=_window;

    这意味着window属性将使用名为_window的ivar(默认情况下ivar名称是属性的名称)

  3. self.window_window 之间有什么区别?”

    前者使用window“getter”方法(即foo = [self window]),后者直接访问ivar。

  4. “为什么我必须多调用一个而不是另一个?”

    通常认为在 dealloc 方法中使用访问器方法是不安全的,这意味着首选使用 ivar。

  1. "Why is there nothing in the @interface"

    The runtime is synthesizing the ivar for you.

  2. "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)

  3. "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.

  4. "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.

情域 2024-11-16 11:00:50

这与 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文