Objective-C 中的 SELF 关键字

发布于 2024-08-16 06:33:13 字数 866 浏览 5 评论 0原文

在我正在创建的项目中,我有各种课程。我的一个类有一个 NSMutableArray 实例,它保存另一个类的对象。我以为我对这个话题已经有了一定的了解,但不知怎的,我的脑海里又变得混乱起来。

当初始化这个类的实例时,我有这个 initilize 方法:

- (MMShowMovement *) initWithMovementName: (NSString *) name andNumber: (NSInteger) number {

 if( [super init] ) {

  [self setMovementTitle: name];
  [self setMovementNumber: number];

  [self setDotArray: [[NSMutableArray alloc] init]];

 }

 return self;

}

稍后,在创建这个类的实例之后,我可以将对象添加到 NSMutableArray“dotArray”中。这是该方法。

- (void) addDot: (MMDot *) dot {

 [dotArray addObject: dot]; 

}

(我知道,这很简单)我想知道,当我在此方法中使用“dotArray”时,我正在访问已调用此方法的类的实例的 dotArray 对象,对吗?或者我也应该在这里使用 self 关键字?

- (void) addDot: (MMDot *) dot {

 [[self dotArray] addObject: dot]; 

}

老实说,我不太确定。我相信是前者,但我不确定为什么。而且在这个方法中没有必要使用 self 关键字,为什么我必须在初始化程序中使用它来访问对象?

In a project I'm creating, I have various classes. One of my classes has an instance of NSMutableArray that holds objects of another one of my classes. I thought I had a firm understanding on this topic, but somehow it got jumbled up in my mind again.

When initializing an instance of this class, I have this initilize method:

- (MMShowMovement *) initWithMovementName: (NSString *) name andNumber: (NSInteger) number {

 if( [super init] ) {

  [self setMovementTitle: name];
  [self setMovementNumber: number];

  [self setDotArray: [[NSMutableArray alloc] init]];

 }

 return self;

}

Later on, after an instance of this class is created, I can add objects to the NSMutableArray "dotArray." Here is that method.

- (void) addDot: (MMDot *) dot {

 [dotArray addObject: dot]; 

}

(I know, its simple) I'm wondering, when I use "dotArray" in this method, I am accessing the dotArray object for the instance of the class for which this method has been invoked, correct? Or should I use the self keyword here also?

- (void) addDot: (MMDot *) dot {

 [[self dotArray] addObject: dot]; 

}

Honestly, I'm not really sure. I believe it is the former, but I'm unsure as to why. And it is not necessary to use the self keyword in this method, why do I have to use it in the initializer to access the object?

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

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

发布评论

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

评论(3

温折酒 2024-08-23 06:33:13

self 是指向当前接收者的指针 - 它指的是接收消息的对象。
您可以使用 self 来访问执行当前方法的对象。

在您的情况下,这意味着如果 MMShowMovement 类型的对象 X 收到 addDot: 消息,则 self 包含指向 X 的指针。
您可以通过三种不同的方式访问 dotArray

[dotArray addObject:dot]; //directly access the instance variable dotArray
[[self dotArray] addObject:dot]; //access dotArray with an accessor method
//--or--
[self.dotArray addObject:dot]; //access dotArray with an accessor method using dot-syntax

如果您使用自动 KVO 通知,则必须使用访问器(而不是直接操作 ivar)来触发通知。
为了确保通过其访问器方法访问变量,您必须添加前缀 self.

self is a pointer to the current receiver - It refers to the object that received a message.
You can use self to get access to the object that performs the current method.

In your case, this means that if object X of type MMShowMovement receives an addDot: message, self contains a pointer to X.
You can access dotArray in three different ways:

[dotArray addObject:dot]; //directly access the instance variable dotArray
[[self dotArray] addObject:dot]; //access dotArray with an accessor method
//--or--
[self.dotArray addObject:dot]; //access dotArray with an accessor method using dot-syntax

If you are using automatic KVO notifications, you have to use the accessors (instead of directly manipulating the ivar) to trigger notifications.
To ensure that a variable is accessed via its accessor method, you have to prefix self.

如日中天 2024-08-23 06:33:13

通常我们会写

self = [super init]  

因为 super 的初始化方法可能返回一个与分配的对象不同的对象。这就是为什么我们在 initxxx 方法中使用 self 而不需要在其他类型的方法中使用 self

我从 Apress 的 Mark Dalrymple 和 Scott Knaster 的《在 Mac 上学习 Objective-C》第 10 章对象初始化一书中清楚地了解了这些内容。

书中摘录:
实例变量位于距隐藏 self 参数固定距离的内存位置。如果从 init 方法返回一个新对象,我们需要更新 self,以便任何后续实例变量引用影响内存中的正确位置。

Usually we'll write

self = [super init]  

Because super's initialization method might return an object that’s not the same as the one that was allocated. So that's why we use self in initxxx method and we don't need to use self in other kinds of methods.

I learned this stuff clearly from the book Learn Objective-C on the Mac, Chap 10 Object Initialization, Mark Dalrymple and Scott Knaster, Apress.

A excerpt from the book:
instance variables are found at a memory location that’s a fixed distance from the hidden self parameter. If a new object is returned from an init method, we need to update self so that any subsequent instance variable references affect the right places in memory.

情仇皆在手 2024-08-23 06:33:13

[self dotArray] 调用对象的“dotArray”选择器(方法)。如果没有这样的方法,你会得到一个错误。您无法以这种方式访问​​实例变量,除非它是一个属性并且您已经为其合成了此 getter 方法。

[dotArray addObject] 只是访问变量并调用 NSMutableArray 指定的内置方法。

[self setMovementTitle:]、[self setMovementNumber:] 和 [self setDotArray:] 都是方法调用。您始终需要一个对象引用来进行方法调用。

在 init 中,您也可以说 [dotArray addObject] (当然是在初始化变量之后)。

[self dotArray] calls the "dotArray" selector (method) of the object. If there is no such method, you'll get an error. You cannot access the instance variable this way, unless it's a property and you've synthesized this getter method for it.

[dotArray addObject] simply accesses the variable and calls a built-in method that is specified by NSMutableArray.

[self setMovementTitle:], [self setMovementNumber:] and [self setDotArray:] are all method calls. You always need an object reference for method calls.

In init, you may as well have said [dotArray addObject] (after initing the variable, of course).

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