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

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

问题描述

我对Objective-C中的属性和实例变量感到困惑。



我是中途离开Aaron Hillegass的Cocoa Programming for Mac OS X 一切都是合乎逻辑的。你会声明一个类像这样:

  @class Something; 

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

东西*东西

IBOutlet NSTextField * myTextField;
}

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




  • 由于其他对象需要操作我们的 name 实例变量,我们使用 @property / @synthesize 为它们生成访问器/ mutator。


  • something

    code>只是一个实例变量,我们将在我们的类中使用,因为没有人需要使用它,我们不创建一对访问器和变异器。

    / li>
  • 我们需要在UI中与文本字段进行交互,因此我们为它声明一个 IBOutlet




所有都很符合逻辑。



但是,在iPhone世界里,事情似乎不同。人们为每个单一实例变量声明属性,声明 IBOutlets 的属性,并使用访问器/ mutator与类内的实例变量进行交互(例如:将写入 [self setName:@Test] ,而不是 name = @Test)。 >

为什么?到底是怎么回事?这些差异是否因iPhone而异?为所有实例变量声明属性的优点是什么?为 IBOutlets 声明属性,并在您自己的类中使用访问器/ mutator?

解决方案

在iPhone世界里,没有垃圾回收器可用。你必须仔细管理内存与引用计数。考虑到这一点,考虑以下之间的区别:

  name = @Test; 

  self.name = @Test; 
//相当于:
[self setName:@Test];

如果直接设置实例变量,值,你不能调整它的保留计数(你应该有 release d手动)。如果您通过属性访问它,它会自动为您处理,同时增加新分配的对象的保留计数。



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


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

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;

  • 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 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.

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

All very logical.

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

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?

解决方案

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";

and

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

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.

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

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

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