What is preferred in Objective-c dot notation or square bracket notation?

余生长醉 提交于 2019-11-27 13:41:40

问题


I'm reading a book - Big Nerd Ranch iOS Programming. It says that dot notation is not recommended as it obfuscates code. I am simultaneously watching a Stanford Course for iOS programming and in it he is using dot notation extensively there. What would you recommend? I personally lean to bracket notation more.

Could you please explain how to covert this code to bracket notation?

self.display.text = [self.display.text stringByAppendingString:digit];

As I understand it should be:

[[self display] setText]:[[[self display] text] stringByAppendingString:digit];

Is it correct?


回答1:


This is a matter of personal choice. There are those that argue that dot notation makes it unclear that messages are being sent (methods are being called) since it looks just like C-style structure element access. The other side of argument is that dot notation is easier to type, easier to read, and more concise.

As someone who has been writing Objective-C since before dot notation was introduced (in Objective-C 2.0), I can understand the arguments on both sides, but prefer to use dot notation myself. That said, I do think it's important for people beginning with Objective-C to understand that dot notation syntax is converted to standard accessor method calls when compiled. I think the authors of the Big Nerd Ranch book probably have a similar attitude, and that's a big part of why they decided to use bracket notation in the book.

So in short, do what you like best. Both are valid, and the choice between the two is essentially a matter of style. No matter which you choose, make sure you understand that both styles produce equivalent compiled code.

EDIT: I forgot to answer the question about converting dot notation to bracket syntax. You're close, but what you've written is wrong and won't actually compile. It should be: [[self display] setText:[[[self display] text] stringByAppendingString:digit]] . If I were writing it I'd split it into two lines (well, really I'd use dot notation):

NSString *stringWithDigit = [[[self display] text] stringByAppendingString:digit];
[[self display] setText:stringWithDigit];

EDIT 2: It has been more than 3 years since I wrote this answer. I just wanted to note that these days, a great many more Apple framework classes have had things that were previously regular methods converted to @properties (e.g. -[NSArray count]) presumably for better Swift interop. This has led me to use dot notation even more liberally than I used to.




回答2:


Here's my opinion:

You should always use dot notation whenever you're dealing with properties. Not only is it quicker to write (self.foo has less characters than [short foo]) but more importantly it makes chained messages easier to understand. For example:

self.myTextField.text.length

is easier to understand than

[[[self myTextField] text] length]

Additionally, it makes it less likely that you'll mess up the punctuation (Making sure that you have the correct number of brackets is oftentimes a pain).

But, as other people have stated above, ultimately it's personal opinion.




回答3:


I know this is an old post but I would like to add a more modern answer.

With the release of swift and the impending demise of our favorite language, the preference should lean towards dot notation. All iOS frameworks have updated their API to favor dot notation (preferring readonly properties in a header to getters). This makes the eventual translation towards swift more fluid.

To be clear, prefer properties for instances where you are exposing getters and/or setters. Luckily Xcode's modern objective-c migration tool will do most of this for you.



来源:https://stackoverflow.com/questions/11474284/what-is-preferred-in-objective-c-dot-notation-or-square-bracket-notation

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!