在Objective-c点表示法或方括号表示法中首选什么? [英] What is preferred in Objective-c dot notation or square bracket notation?

查看:92
本文介绍了在Objective-c点表示法或方括号表示法中首选什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在读一本书-Big Nerd Ranch iOS编程. 它说不建议使用点号表示法,因为它会混淆代码. 我同时正在观看斯坦福大学的iOS编程课程,并且在那里他广泛使用点符号.你会推荐什么?我个人更倾向于括号表示法.

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];

据我所知应该是:

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

对吗?

推荐答案

这是个人选择的问题.有些人认为点符号使不清楚消息是否正在发送(正在调用方法),因为它看起来就像C样式的结构元素访问一样.论点的另一面是,点符号更易于键入,易于阅读且更简洁.

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.

作为自从在引入点符号之前(在Objective-C 2.0中)开始编写Objective-C的人,我可以理解双方的论点,但更喜欢自己使用点符号.就是说,我确实认为对于以Objective-C开头的人们来说,理解点符号语法在编译时会转换为标准访问器方法调用非常重要.我认为《大书呆子牧场》一书的作者可能也有类似的态度,这就是为什么他们决定在书中使用括号表示法的重要原因.

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.

我忘了回答有关将点表示法转换为方括号语法的问题.您已经接近了,但是您所写的内容是错误的,实际上无法编译.它应该是:[[self display] setText:[[[self display] text] stringByAppendingString:digit]].如果我正在编写它,则将其分为两行(嗯,实际上我会使用点表示法):

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];

自从我写了这个答案以来已经有3年多了.我只想指出,这些天来,更多的Apple框架类将以前的常规方法转换为@properties(例如-[NSArray count]),大概是为了更好地实现Swift互操作.这使我比以前更加自由地使用点符号.

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.

这篇关于在Objective-c点表示法或方括号表示法中首选什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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