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

后端 未结 3 1231
隐瞒了意图╮
隐瞒了意图╮ 2020-12-14 07:03

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

相关标签:
3条回答
  • 2020-12-14 07:09

    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.

    0 讨论(0)
  • 2020-12-14 07:23

    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.

    0 讨论(0)
  • 2020-12-14 07:29

    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.

    0 讨论(0)
提交回复
热议问题