何时在 Objective-C 中使用 `self`? [英] When to use `self` in Objective-C?

查看:67
本文介绍了何时在 Objective-C 中使用 `self`?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

现在我使用 Objective-C 已经 5 个多月了,我也在 App Store 发布了我的第一个应用程序,但我仍然对该语言的核心功能有疑问.

It's now more than 5 months that I'm in Objective-C, I've also got my first app published in the App Store, but I still have a doubt about a core functionality of the language.

我什么时候应该使用 self 访问 iVars,什么时候不应该使用?

When am I supposed to use self accessing iVars and when I'm not?

发布插座时,您在 viewDidUnload 中写入 self.outlet = nil,而不是在 dealloc 中写入 [outlet release].为什么?

When releasing an outlet you write self.outlet = nil in viewDidUnload, instead in dealloc you write [outlet release]. Why?

推荐答案

当你编写 self.outlet = nil 方法 [self setOutlet:nil]; 被调用.当您编写 outlet = nil; 时,您可以直接访问变量 outlet.

When you write self.outlet = nil the method [self setOutlet:nil]; is called. When you write outlet = nil; you access variable outlet directly.

如果你使用 @synthesize outlet; 那么方法 setOutlet: 会自动生成并且它在分配新对象之前释放对象,如果你将属性声明为 @property (保留) NSObject 出口;.

if you use @synthesize outlet; then method setOutlet: is generated automatically and it releases object before assigning new one if you declared property as @property (retain) NSObject outlet;.

这篇关于何时在 Objective-C 中使用 `self`?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆