4

有人可以澄清这里发生了什么。我有一个名为:brainModel 的类,它又有一个名为:operandStack 的 NSArray。我只是访问向它发送消息“removeAllObjects”的数组

self.brainModel.operandStack.removeAllObjects;

但是使用点表示法它会给我一个警告“未使用的属性访问结果-getter 不应该用于副作用”这到底是什么意思?

使用这样的嵌套括号语法不会给出警告:

 [[[self brainModel]operandStack]removeAllObjects];

顺便说一句,两者都有效......这与错误使用点表示法有什么关系吗?或者在这样的消息传递对象时使用点表示法被认为是一种好习惯 - 向它发送像“removeAllObjects”这样的参数。

4

3 回答 3

5

removeAllObjects is not a property; it's a method.

Using property-access notation works because properties are usually accessed using a method of the same name. However, it is expected that getting a property's value will not change the object which contains the object (or make any other changes), which is not the case with removeAllObjects. These are the "side effects" that the compiler is referring to.

Probably, you would want to perform this call instead:

[self.brainModel.operandStack removeAllObjects];

This gets the brainModel property of self, then the operandStack property of self.brainModel, then calls removeAllObjects on it.

于 2012-08-15T15:28:27.140 回答
1

removeAllObjects is a method. You cannot access methods through dot notation; only properties.

于 2012-08-15T15:28:32.947 回答
0

You don't need to declare each and every method as a property - especially not if they are modifying the object. Getters should be viewed as accessors to a property (without exposing the backing ivar directly). Methods that are 'actions', to say, are to be declared as such (i. e. declared without the @property keyword and called using brackets instead of the dot notation).

于 2012-08-15T15:30:41.123 回答