iOS First Application“self.userName = textField.text”。什么时候使用自己 [英] iOS First Application "self.userName = textField.text". When to use self

查看:130
本文介绍了iOS First Application“self.userName = textField.text”。什么时候使用自己的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下是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 =问候;
[问候发布];
}

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



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



<这两个是等价的吗?
自己是不必要的,因为点符号已经存在已经访问过get方法吗?

解决方案

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



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


这两个是等价的吗?自我
是不必要的,因为点符号是
已经有
访问get方法?


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


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?

解决方案

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.

这篇关于iOS First Application“self.userName = textField.text”。什么时候使用自己的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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