问题描述
aclass.h
@interface aClass : NSObject {
NSString *name;
}
@property (nonatomic, retain) IBOutlet NSString *name;
@end
aclass.m
@implementation aClass
@synthesize name;
- (void)dealloc {
[name release];
[super dealloc];
}
- (void)test1 {
name = @"hello";
}
- (void)test2 {
self.name = @"hello";
}
以上面为例。有人可以解释 name = @hello
和 self.name = @hello
之间的区别吗?谢谢!
Take above as an example. Could someone please explain the difference between name = @"hello"
and self.name = @"hello"
? Thanks!
编辑:后续问题:
推荐答案
请注意,这个帖子已经老了!
这篇文章来自过去十年。
BE AWARE, THIS POST IS OLD !
This post is from the previous decade.
请务必阅读下面的重要脚注,欢呼!!
Be sure to read the important footnote down below, cheers!!
当你刚刚开始时,真的很难理解这一切。
It's really difficult to understand all this, when you're just getting started.
以下是一些简单,实用的经验法则 FOR BEGINNERS 。
Here are some SIMPLE, PRACTICAL rules of thumb FOR BEGINNERS.
重复一下,这篇文章是 FOR BEGINNERS 。
To repeat, this post is FOR BEGINNERS.
这里的目标是允许你快速从起跑线移动到能够在大多数情况下自信地使用系统。
The aim here is to allow you to quickly move from the starting line, to being able to confidently use the system in most situations.
以后,你可以真正了解这些内部运作方式起诉。
Later, you can really learn about the inner workings of these issues.
(1)不要说 name = @hello
。 总是说 self.name = @hello
。在项目范围内搜索 name
并确保始终说 self.name
而不是名称
,当你设置或更改它时。
(1) Don't ever say name=@"hello"
. Always say self.name=@"hello"
. Do a project-wide search for name
and ensure you always say self.name
and not name
, when you set it or change it.
(2)你知道关于内存管理,初始化,发布等所有令人生气的事情上。如果你使用自己的东西,它会照顾你所有的东西。很酷?
(2) You know all that infuriating stuff about memory management, initialising, releasing and so on. If you use the self thingy, it takes care of all that for you. Cool huh?
(3)自我特别有用,因为你可以轻松地改变字符串(或其他任何东西)你去吧所以,这样做完全没问题,
(3) The self thingy is particularly useful because you can easily "change" the string (or whatever it is) as you go along. So, it's totally OK to do this,
self.name=@"aa";
self.name=@"bb";
self.name=@"cc";
而(总之)你永远不会出于任何原因这样做 ...
name=@"aa";
name=@"bb";
name=@"cc";
(*)关于你的文字问题,请解释名称之间的区别= @hello
和 self.name = @hello?
这很容易做到。
( * ) Regarding your literal question, "please explain the difference between name = @"hello"
and self.name = @"hello"?"
This is easy to do.
第一个是只是设置一个变量。你知道,在生活很简单且我们13岁的时候,完全像x = 42
。
The first one is just setting a variable. You know, exactly like "x=42"
in the old days when life was simple and we were 13 years old.
第二个是完全不同的,特别是它正在调用一个复杂的例程(称为setter)来为你做很多令人惊讶和惊人的事情。
The second one is completely different, specifically it is calling a complicated routine (known as "the setter") to do a whole lot of amazing and astonishing stuff for you.
这是你问题的字面答案。 第一个只是设置变量(并且不要忘记,有很多指针和其他奇怪的东西涉及,并且通常你肯定不能像那样设置指针willy-nilly)。 第二个实际上是一个很复杂的例程,因此会为你做很多事情。
So that is the literal answer to your question. The first one just sets the variable (and don't forget, there are a whole lot of pointers and other weird stuff involved, and as a rule you certainly can not just set pointers willy-nilly like that). The second one actually calls a big complicated routine and hence does a whole lot of stuff for you.
再一次,第二个确实是比如说......
Once again, the second one is exactly like saying...
[name bigComplicatedRoutineHere:@"hello"];
...始终记住语法 self是非常有帮助的。 ...
实际上是在调用例程。
...it is very helpful to always remember that the syntax self. ...
is literally calling a routine.
事实上,一些有关该主题的思想家认为这是一个愚蠢的当他们引入这个 self.X
语法来表示 [X complexThingHere]
时的想法。它引起了很多困惑,并且每个初学者都会问你究竟是在问什么。
Indeed, some thinkers on the topic thought it was a dumb idea when they introduced this self.X
syntax to mean [X complicatedThingHere]
. It introudces a lot of confusion, and every beginner asks exactly what you are asking.
就我个人而言,花了我9年多的时间才得到这个清醒在我的脑海里。 :-)所以再次强调你必须记住,当你说 self.x
时,事实上,你实际上是在调用例程。
Personally, it took me over nine years to get this clear in my head. :-) So again, I emphasise that you must remember that when you say self.x
, in fact, you are actually calling a routine.
重复:自我点语法实际上调用例程。 (事实上,我相信其中一个预处理器只是将其扩展为 [x amazingStuffHere]
。)
To repeat: the "self dot" syntax in fact calls a routine. (Indeed I believe one of the preprocessors simply expands it to [x amazingStuffHere]
. )
我试过在学习内存管理,属性等的同时,以一种让您继续前进并允许您前进和使用更多功能的方式回答。如果您比这篇文章更先进,请忽略它。
I have tried to answer in a way that will keep you going and allow you to advance and use more features, while you learn about memory management, properties, and so on. If you are more advanced than this post, just ignore it.
请注意,此帖旨在为初学者提供建议以启用它们继续前进而不会被激怒。希望它有所帮助!
Please note that this post is meant to be advice for beginners to enable them to keep going and not get infuriated. Hope it helps!
注意,这篇文章现在已经五年了!它已被成千上万的初学者阅读,并有许多后续问题等。请注意,今天在新的ARC世界。在某种程度上:如果你是初学者:你应该只使用!! 属性。即,随时随地使用self.whatever。无论如何,请注意这篇文章中的信息基本上是历史性的,并且每天都变得越来越多。当然,不言而喻,一旦您成为专家,您将需要并且将了解所有这些细节。希望它对某人有所帮助。
Note, this post is five years old now! It's been read by thousands of beginners and there have been many followup questions etc. Please note that, today in the new "ARC world". To some extent: if you're a beginner: you should pretty much only use!! properties. ie, use "self.whatever" at all times, everywhere. In any event, just be aware that the information in this post is "largely historic" and is becoming more so every day. Of course, it goes without saying that once you are an expert, you will need to and will understand every subtle detail of all this. Hope it helps someone.
这篇关于self.ivar和ivar之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!