为什么 iOS SDK 教程中的所有内容都是属性?

发布于 2024-10-05 05:31:03 字数 544 浏览 7 评论 0原文

在我从 Apple 和其他地方在线阅读的大多数 iOS SDK 教程中,许多实例变量都被设置为属性,即使它们只能从自己的类中访问。

Facebook 最新的 iOS SDK 中的 EG 鼓励用户在应用程序委托中维护一个名为 facebook 的 Facebook 类实例。 facebook" 是一个属性。然后在整个示例代码中将其称为“self.facebook”。但是应用程序委托外部的任何地方都不会引用“facebook”属性。

这是为什么?什么我错过了吗?如果实例变量仅在其自己的类中使用,我倾向于将其设为属性,以便它不公开可见,并将其简单地称为facebook 而不是类中的 self.facebookself->facebook

坦率地说,即使我需要 facebook 属性,我想我也会这样做。而是在应用程序委托中将其简单地称为“facebook”而不是“self.facebook”

那么为什么我到处都能看到属性呢?

In most of the iOS SDK tutorials I've read online both from Apple and elsewhere, so many instance variables are made properties, even when they are only accessed from within their own class.

E.G. in Facebook's latest iOS SDK a user is encouraged to maintain an instance of the Facebook class called facebook in the app delegate. facebook" is a property. This is then referred to throughout the sample code as "self.facebook". But the "facebook" property is not referenced anywhere from outside the app delegate.

Why is this? What am I missing? If an instance variable is only used within its own class, my inclination would be not to make it a property so that it is not publicly visible, and refer to it simply as facebook rather than self.facebook or self->facebook from within the class.

Frankly, even if I needed the facebook property, I think I'd rather refer to it within the app delegate as simply "facebook" rather than "self.facebook".

So what gives? Why am I seeing properties everywhere?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(6

顾冷 2024-10-12 05:31:04

如果您引用 facebook,那么它将直接访问该变量,如果您访问 self.facebook,那么它将通过 setter/getter 方法。

结果是,如果您需要改变一个对象,或者在稍后的某个时间点,您需要在更改值的过程中做其他事情(即装箱),那么您可以将其放入 setter 方法中并减少必须做的事情它无处不在。这是一件好事。

if you refer to facebook, then it will access the variable directly, if you access self.facebook, then it will go through the setter/getter methods.

the result is that if you need to mutate an object or at somepoint later in time, you need to do other things during the process of changing the value(ie boxing it) then you can put this in the setter method and reduce having to do it everywhere. its a good thing.

优雅的叶子 2024-10-12 05:31:04

它会自动为您创建 getter/setter。
另外,这里有一篇很好的文章解释了它属性

It creates the getter/setter for you automatically.
Also, here is a good post explaining it properties

长伴 2024-10-12 05:31:03

属性与 iVar 是否公开暴露没有任何关系。相反,属性创建了管理保留和促进封装的访问器方法。

如果您想要私有属性,只需在 @private 指令下声明 ivar,然后在实现文件中定义属性即可。

Properties don't have anything to do with whether an iVar is publicly exposed or not. Instead properties create accessors methods that manage retention and foster encapsulation.

If you want a private property just declare the ivar under the @private directive and then define the properties in the implementation file.

电影里的梦 2024-10-12 05:31:03

为什么应该使用属性(您可以将属性设置为私有,如 TechZen 所说)的一个很好的例子是在 viewDidLoad 中创建新实例。

人们总是这样做,因为每个视图控制器实例都会调用一次。或者,所以你认为......实际上,由于应用程序收到内存警告时发生的情况,每个实例可能会多次调用 viewDidLoad 类。

例如,您可能会编写如下代码:

- (void) viewDidLoad
{ myArray = [[NSMutableArray alloc] init]; }

效果很好 - 直到再次调用它,然后您就会发生泄漏。您可以在分配 myArray 之前释放它以防万一 - 但问题是这一步很容易被忘记。

如果您为每个实例变量使用属性,那么您的代码将如下所示:

- (void) viewDidLoad
{ self.myArray = [NSMutableArray array]; }

现在您不会出错了。如果多次调用 viewDidLoad,旧数组将被释放,新数组将取代它,而不会泄漏旧数组。

One great example of why you should use properties (you can make properties private as TechZen noted), is creating new instances in viewDidLoad.

People do this all the time, because it's called once per instance of a view controller. Or, so you think... in reality because of what happens when you application gets a memory warning, a viewDidLoad class could be called multiple times per instance.

So for example you might well write code like:

- (void) viewDidLoad
{ myArray = [[NSMutableArray alloc] init]; }

Works great - until it's called again, then you have a leak. You could release myArray before you assign it just in case - but the trouble is that's an easy step to forget.

If you use properties for every instance variable though, then your code looks like this:

- (void) viewDidLoad
{ self.myArray = [NSMutableArray array]; }

Now you can't get it wrong. If viewDidLoad is called multiple times, the old array will be released and a new one will go in its place, without leaking the old array.

一念一轮回 2024-10-12 05:31:03

通常最好通过访问器访问实例变量,而不是直接访问。属性创建这些访问器,并且还提供适当的键值更改通知和原子访问等功能。

Usually it's better to access to the instance variable through an accessor, rather than directly. Properties create these accessors, and also provide things like proper key-value change notifications and atomic access.

一个人练习一个人 2024-10-12 05:31:03

属性可以生成 getter 和 setter。特别是,Setter 可以帮助(半)自动管理对象的保留/释放计数,从而减少某些类型的潜在内存错误。

非对象的 getter 和属性主要是为了编码风格的正交性,尽管它们确实有可能在未来的代码扩展或重用中用于修改器、键值通知等。

可能存在一些效率差异/缺点,但这取决于所使用的特定 ObjC 运行时。

Properties can generate getters and setters. Setters, in particular, can help manage retain/release counts of objects (semi)automatically, and thus reduce certain types of potential memory bugs.

Getters and properties for non-objects are mostly for orthogonality of coding style, although they do have potential to be used for mutators, key-value notifications, etc. in some future extension or reuse of the code.

There may be some efficiency differences/disadvantages, but that depends on the particular ObjC run-time in use.

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