self.ivar 和 ivar 之间的区别? [英] Difference between self.ivar and ivar?

查看:25
本文介绍了self.ivar 和 ivar 之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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!

后续问题:如何为 ivar 编写我自己的 setter,即:self.ivar = ...?

推荐答案

请注意,这篇文章已经过时了!

这篇文章是过去十年的.

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.

这里有一些简单实用的经验法则适合初学者.

Here are some SIMPLE, PRACTICAL rules of thumb 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 而不是 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) 你知道所有关于内存管理、初始化、释放等令人生气的东西.如果您使用 self thingy,它会为您处理所有这些.很酷吧?

(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) self thingy 特别有用,因为您可以轻松地改变";字符串(或任何它)随着您的进行.所以,这样做完全没问题,

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

( * ) 关于你的字面问题,请解释一下 name = @"hello"self.name = @"hello"?" 之间的区别code> 这很容易做到.

( * ) 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.

这就是你问题的字面答案.第一个只是设置变量(别忘了,这里有很多指针和其他奇怪的东西,通常你当然不能只是像那样随意设置指针).第二个实际上调用了一个大而复杂的例程,因此为您做了很多事情.

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.

就我个人而言,我花了九年多的时间才把这一点弄清楚.:-) 所以我再次强调,你必须记住,当你说 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!

注意,这篇文章已经 5 岁了了!它已被成千上万的初学者阅读,并且有许多后续问题等.请注意,今天在新的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 之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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