声明属性的点符号与消息符号 [英] Dot notation vs. message notation for declared properties

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

问题描述

我们现在有了属性的点"符号.我见过各种 backforths 关于点表示法与消息表示法的优点.为了不让回答受到污染,我不会以任何一种方式回答问题.

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.

这篇关于声明属性的点符号与消息符号的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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