NSArray(和其他可可类型)@property值 [英] NSArray (and other Cocoa types) @property values

查看:88
本文介绍了NSArray(和其他可可类型)@property值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在调试同事编写的代码的过程中,我偶然发现了以下使我感到迷惑的内容:

While in the process of debugging code written by a co-worker, I stumbled across the following that has me mystified:

NSMutableArray *array = [NSMutableArray array];
NSUInteger arrayCount = array.count;

为什么这样做?它也适用于 NSDictionary 和其他类型,但是在文档或Cocoa标头中都找不到这些 @property 定义。

Why does this work? It also works for NSDictionary and other types, but nowhere in the documentation nor Cocoa headers can those @property definitions be found.

搜索 NSArray属性不会产生很多有用的结果,所以我向SO提出了一个肯定会是一个非常尴尬的问题。 p>

Googling for "NSArray property" doesn't yield many useful results, so I'm reaching out to SO for what will surely be a very embarrassing question.

推荐答案

它之所以有效,是因为点语法与属性无关。它只是句法糖(尽管我不喜欢它,所以也许是句法盐)。

It works because dot syntax has nothing to do with properties. It is simply syntactic sugar (though I don't like it, so perhaps it's "syntactic salt").

当您使用点语法作为表达式的右值时(或等号右边的表达式),它简单地变成:

When you use dot syntax as the rvalue to an expression (or the expression to the right of the equal sign), it simple turns:

bar = myObject.thing;

进入

bar = [myObject thing];

当点在等号的左侧(作为左值)时,它将变成塞特犬。因此:

When the dot is to the left of the equal sign (as an lvalue), it turns it into the setter. So:

myObject.thing = 42;

成为

[myObject setThing:42];

是的,您可以执行 myObject.retain 。但是您永远都不要这样做。您仅应将点语法用作声明属性的访问器(即,已通过 @property 明确声明的事物)。 <插入有关完全不应该使用点语法的注释。>

So yes, you can do things like myObject.retain. But you should never ever do that. You should only ever use dot syntax as accessors to declared properties (ie, things that have been explicitly declared via @property). <insert remark about how you should never use dot syntax at all.>

有关更多信息,检出有关点语法的文档(特别是不正确使用部分)。

For more information, checkout out the documentation on dot syntax (specifically the "incorrect use" section).

这篇关于NSArray(和其他可可类型)@property值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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