Objective-C 中的属性和实例变量 [英] Properties and Instance Variables in Objective-C

查看:24
本文介绍了Objective-C 中的属性和实例变量的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我对 Objective-C 中的属性和实例变量很困惑.

I'm rather confused about properties and instance variables in Objective-C.

我已经完成了 Aaron Hillegass 的Mac OS X 的 Cocoa 编程",一切都很合乎逻辑.你可以声明一个这样的类:

I'm about half-way through Aaron Hillegass's "Cocoa Programming for Mac OS X" and everything is logical. You would declare a class something like this:

@class Something;

@interface MyClass : NSObject {
    NSString *name;
    NSArray *items;

    Something *something;

    IBOutlet NSTextField *myTextField;
}

@property (nonatomic, retain) NSString *name;
@property (nonatomic, retain) NSArray *items;

  • 由于其他对象需要操作我们的nameitems实例变量,我们使用@property/@synthesize 为它们生成访问器/修改器.在我们的类中,我们不使用访问器/修改器——我们只是直接与实例变量交互.

    • Since other objects need to manipulate our name and items instance variables, we use @property/@synthesize to generate accessors/mutators for them. Within our class, we don't use the accessors/mutators—we just interact with the instance variable directly.

      something 只是我们将在我们的类中使用的一个实例变量,由于没有其他人需要使用它,我们不创建一对访问器和它的变异器.

      something is just an instance variable that we're going to use in our class, and since no one else needs to use it, we don't create a pair of accessors and mutators for it.

      我们需要与 UI 中的文本字段进行交互,因此我们为它声明一个 IBOutlet,连接它,然后我们就完成了.

      We need to interact with a text field in our UI, so we declare an IBOutlet for it, connect it, and we're done.

      一切都非常合乎逻辑.

      然而,在 iPhone 的世界里,情况似乎有所不同.人们为每个实例变量声明属性,为 IBOutlets 声明属性,并使用访问器/修改器与类的实例变量交互(例如,他们会编写 [self setName:@"Test"] 而不是 name = @"Test").

      However, in the iPhone world, things seem to be different. People declare properties for every single instance variable, declare properties for IBOutlets, and use accessors/mutators to interact with instance variables within the class (e.g. they would write [self setName:@"Test"] rather than name = @"Test").

      为什么?到底是怎么回事?这些差异是 iPhone 特有的吗?为所有实例变量声明属性、为 IBOutlets 声明属性以及在您自己的类中使用访问器/修改器有什么优点?

      Why? What is going on? Are these differences iPhone-specific? What are the advantages of declaring properties for all instance variables, declaring properties for IBOutlets, and using accessors/mutators within your own class?

      推荐答案

      在 iPhone 世界中,没有可用的垃圾收集器.您必须使用引用计数仔细管理内存.考虑到这一点,请考虑以下区别:

      In the iPhone world, there's no garbage collector available. You'll have to carefully manage memory with reference counting. With that in mind, consider the difference between:

      name = @"Test";
      

      self.name = @"Test";
      // which is equivalent to:
      [self setName: @"Test"];
      

      如果直接设置实例变量,没有事先考虑,你将失去对前一个值的引用,并且你不能调整它的保留计数(你应该手动released它).如果您通过属性访问它,它将自动为您处理,同时增加新分配对象的保留计数.

      If you directly set the instance variable, without prior consideration, you'll lose the reference to the previous value and you can't adjust its retain count (you should have released it manually). If you access it through a property, it'll be handled automatically for you, along with incrementing the retain count of the newly assigned object.

      基本概念不是特定于 iPhone 的,但它在没有垃圾收集器的环境中变得至关重要.

      The fundamental concept is not iPhone specific but it becomes crucial in an environment without the garbage collector.

      这篇关于Objective-C 中的属性和实例变量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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