声明属性的点表示法与消息表示法

Dot notation vs. message notation for declared properties(声明属性的点表示法与消息表示法)

本文介绍了声明属性的点表示法与消息表示法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我们现在有了属性的点"符号.我见过各种 back 和 forths 关于点表示法与消息表示法的优点.为了不影响回答,我不会在问题中以任何方式回答.

We now have the "dot" notation for properties. I've seen various back and forths about the merits of dot notation vs. message notation. To keep the responses untainted I'm not going to respond either way in the question.

您对用于属性访问的点表示法与消息表示法有何看法?

What is your thought about dot notation vs. message notation for property accessing?

请尽量把重点放在 Objective-C 上——我要提出的一个偏见是 Objective-C 就是 Objective-C,所以你认为它像 Java 或 JavaScript 的偏好是无效的.

Please try to keep it focused on Objective-C - my one bias I'll put forth is that Objective-C is Objective-C, so your preference that it be like Java or JavaScript aren't valid.

有效的评论与技术问题(操作顺序、转换优先级、性能等)、清晰度(结构与对象性质,赞成和反对!)、简洁性等有关.

Valid commentary is to do with technical issues (operation ordering, cast precedence, performance, etc), clarity (structure vs. object nature, both pro and con!), succinctness, etc.

请注意,我在代码质量和可读性方面具有严格的质量和可读性,曾参与过代码约定和质量至关重要的大型项目(一次写入,读取一千次范例).

Note, I'm of the school of rigorous quality and readability in code having worked on huge projects where code convention and quality is paramount (the write once read a thousand times paradigm).

推荐答案

不要使用点表示行为.使用点来访问或设置属性之类的东西,通常是声明为属性的属性.

Do not use dot for behavior. Use dot to access or set attribute like stuff, typically attributes declared as properties.

x = foo.name; // good
foo.age = 42; // good

y = x.retain; // bad

k.release; // compiler should warn, but some don't. Oops.

v.lockFocusIfCanDraw; /// ooh... no. bad bad bad

对于刚接触 Objective-C 的人,我建议不要将点用于声明为 @property 的东西之外的任何东西.一旦你对语言有了感觉,就去做感觉对的事情.

For folks new to Objective-C, I would recommend not using the dot for anything but stuff declared as @property. Once you have a feel for the language, do what feels right.

例如,我发现以下内容非常自然:

For example, I find the following perfectly natural:

k = anArray.count;
for (NSView *v in myView.subviews) { ... };

您可以预期 clang 静态分析器将增加允许您检查点仅用于某些模式或不用于某些其他模式的能力.

You can expect that the clang static analyzer will grow the ability to allow you to check that the dot is being used only for certain patterns or not for certain other patterns.

这篇关于声明属性的点表示法与消息表示法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:声明属性的点表示法与消息表示法

基础教程推荐