iOS 第一个应用程序“self.userName = textField.text”。何时使用自我

发布于 2024-11-11 13:51:28 字数 549 浏览 3 评论 0原文

以下是 Apple 的“您的第一个 iOS 应用程序”文档中的代码片段。

- (IBAction)changeGreeting:(id)sender {

self.userName = textField.text;

NSString *nameString = self.userName;
if ([nameString length] == 0) {
    nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
label.text = greeting;
[greeting release];
}

我知道 self.username 调用合成的 set 方法(很重要,因为它有一个复制标志)。

为什么textField.text和label.text不是self.textField.text和self.label.text。

两者等价吗? self 是否不必要,因为点符号已经存在,可以访问 get 方法?

Here is a code snippet from Apple's "Your First iOS Application" document.

- (IBAction)changeGreeting:(id)sender {

self.userName = textField.text;

NSString *nameString = self.userName;
if ([nameString length] == 0) {
    nameString = @"World";
}
NSString *greeting = [[NSString alloc] initWithFormat:@"Hello, %@!", nameString];
label.text = greeting;
[greeting release];
}

I understand that self.username calls the synthesized set method (important since it has a copy flag).

Why is textField.text and label.text not self.textField.text and self.label.text.

Are the two equivalent?
Is the self unnecessary since the dot notation is there already which would already access the get methods?

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

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

发布评论

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

评论(4

我一直都在从未离去 2024-11-18 13:51:28

是的:在这种情况下,textField.text 相当于 self.textField.text,因为合成的 getter 只是返回文本字段。大概苹果公司已经追求简洁,因为他们希望代码可读。不过,我赞成你的方法:对于属性,坚持使用访问器方法是一个好习惯,以防你想要自定义它们。

请注意,该属性是独立于内部变量的实体。 Apple 的风格是给它们赋予相同的名称,但一些程序员喜欢通过给内部变量下划线前缀来分隔这两个概念。在这种情况下,_textField.text 将给出与 self.textField.text 相同的结果。但只有第二个会访问类为文本字段生成的 getter 方法 - 第一个是行使它作为类内部代码的一部分的权利来直接访问内部变量。

Yes: textField.text is equivalent to self.textField.text in this case, because the synthesised getter simply returns the text field. Presumably Apple have gone for terseness because they want the code to be readable. I'd favour your approach though: with properties, it's a good habit to stick to the accessor methods, in case you ever want to customise them.

Note that the property is a separate entity from the internal variable. Apple's style is to give them both the same name, but some programmers like to separate the two concepts by giving internal variables underscore prefixes. In that case, _textField.text would give the same result here as self.textField.text. But only the second would be accessing your class's generated getter method for the text field - the first is exercising its right as a piece of class-internal code to access the internal variable directly.

提笔落墨 2024-11-18 13:51:28

不,它们不一样。在您提供的代码中,textField.text 转换为 [textField text],即获取 < 指向的对象的 text 属性代码>textField ivar.另一方面,self.textField.text 转换为 [[self textField] text],即调用当前对象的 textField 访问器,并调用结果的 text 访问器。

最终结果通常应该是相同的。同时拥有 ivar 和名为 textField 的属性,并且让该属性返回 ivar 以外的内容,这会有点奇怪。

两者等价吗?是自我
不必要的,因为点符号是
已经在那里了
访问 get 方法?

如上所述,结果相似,但含义不同。使用访问器(即 self.textField.text)是首选样式,但为所有内容添加前缀 self. 似乎也有点乏味。如果要重复使用属性,一种可能的补救措施是调用属性访问器一次并将结果保留在局部变量中。

No, they're not the same. In the code you provided, textField.text translates to [textField text], i.e. gets the text property of the object pointed to by the textField ivar. self.textField.text, on the other hand, translates to [[self textField] text], i.e. calls the current object's textField accessor, and calls the text accessor of the result.

The end result should usually be the same. It would be somewhat strange to have both an ivar and a property named textField and to have the property return something other than the ivar.

Are the two equivalent? Is the self
unnecessary since the dot notation is
there already which would already
access the get methods?

As explained above, the results are similar, but the meaning is different. Using the accessor (i.e. self.textField.text) is the preferred style, but prefixing everything with self. can seem a little tedious too. One possible remedy if you're going to use a property repeatedly is to call the property accessor once and keep the result in a local variable.

您的好友蓝忘机已上羡 2024-11-18 13:51:28

是的,两者是相同的。您可以使用 self.label.text 或 label.text (以两者为准),因为两者都指向同一对象。

Yes, Both are the same. You may use self.label.text or label.text (whichever) as both point to the same object.

把梦留给海 2024-11-18 13:51:28

您自己说过,它们是等效的,因为 self.labellabel 将指向同一个对象。

为了更清晰起见,我更愿意使用 self.label 版本,但这完全是一种编码实践。

需要指出的是,当您使用 self.label 时,性能会受到影响,因为您正在调用一个方法(该方法不是免费的)。然而,在大多数阅读世界的情况下,性能影响并不明显(很高兴知道)。

You said it yourself, they are equivalent since self.label and label will point to the same object.

I would prefer to use the self.label version for more clarity but that's entirely a coding practice.

It's important to point out that there's a performance hit when your use the self.label since you're calling a method (which is not free). However, in most read-world cases, the performance hit is not noticeable (just good to know).

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