Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
313 views
in Technique[技术] by (71.8m points)

iphone - Objective-C: When to call self.myObject vs just calling myObject

This little bit of syntax has been a bit of a confusion for me in Objective-C.

When should I call self.myObject vs just calling myObject?

It seems redundant however they are not interchangeable.

Would someone please enlighten me?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

If you're just accessing them, there's not much reason to use self.member. If you're doing assignment, it's good if you're doing more than the simple @property (assign) parameter--for instance, retain, copy, etc, so it can save on code you're writing. An example:

myObject = anotherObject;
self.myObject = anotherObject;

The second choice will ensure that you're actually assigning the object the way you want (getting a copy, increasing the retain count, etc.). It's no different from [self setMyObject:anotherObject].

Since the dot-notation gets substituted for a message by the compiler (similar to how x[5] becomes *(x + 5*sizeof(x)) in regular array work), there's no overhead or extra efficiency in using dot-notation over regular messages.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...