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

后端 未结 3 1325
隐瞒了意图╮
隐瞒了意图╮ 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: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.

提交回复
热议问题