使用点符号表示实例方法

时间:2022-10-20 22:30:14

I was looking at a piece of code today and notice that this particular coder use dot notation to access instance methods (these methods don't take values, they just return value).

我今天看了一段代码,注意到这个特殊的编码器使用点符号来访问实例方法(这些方法不取值,它们只是返回值)。

For example:

例如:

@interface MyClass : NSObject {

}
-(double)add;
@end

@implementation MyClass
-(double)valueA {
    return 3.0;
}

-(double)valueB {
    return 7.0;
}

-(double)add {
    return self.valueA + self.valueB;
}    
@end

He did this through out his code and the compiler doesn't complain, but when I try it in my code like the example above I get the following error: "Request for member "valueA" in something not a structure or union". What am I missing, any idea?

他这样做是通过他的代码,而编译器没有抱怨,但是当我在我的代码中尝试时,比如上面的例子,我得到了下面的错误:“请求成员”valueA“在某种非结构或联合”中。我错过了什么,知道吗?

1 个解决方案

#1


7  

The dot syntax is usually applied to declared properties but that’s not mandatory. Using obj.valueA and obj.valueB does work.

点语法通常用于声明属性,但这不是强制的。使用obj。valueA obj。valueB工作。

The error message you’re getting is probably due to the object not having explicit type MyClass *. For example, the following works:

您得到的错误消息可能是由于对象没有显式类型MyClass *。例如:

MyClass *obj1 = [MyClass new];
NSLog(@"%f %f %f", obj1.valueA, obj1.valueB, [obj1 add]);

On the other hand:

另一方面:

MyClass *obj1 = [MyClass new];
NSLog(@"%f %f %f", obj1.valueA, obj1.valueB, [obj1 add]);

id obj2 = obj1;
NSLog(@"%f %f %f", obj2.valueA, obj2.valueB, [obj2 add]);

gives:

给:

error: request for member ‘valueA’ in something not a structure or union
error: request for member ‘valueB’ in something not a structure or union

because obj2 has type id, so the compiler doesn’t have enough information to know that .valueA and .valueB are actually the getter methods -valueA and -valueB. This can happen if you place objects of type MyClass in an NSArray and later retrieve them via -objectAtIndex:, since this method returns a generic object of type id.

因为obj2有类型id,所以编译器没有足够的信息知道。valuea和。valueb实际上是getter方法-valueA和-valueB。如果将MyClass类型的对象放在NSArray中,然后通过-objectAtIndex:检索它们,就会发生这种情况,因为该方法返回类型id的泛型对象。

To appease the compiler, you need to cast the object to MyClass * and only then use the dot syntax. You could accomplish this by:

为了使编译器满意,您需要将对象转换为MyClass *,然后才使用点语法。你可以通过:

MyClass *obj2 = obj1;
// or
MyClass *obj2 = [someArray objectAtIndex:someIndex];
// and then
obj2.valueA

or, if obj2 is declared as id:

或者,如果obj2被声明为id:

((MyClass *)obj2).valueA

or, if the object is returned by a method whose return type is id:

或者,如果对象被返回类型为id的方法返回:

((MyClass *)[someArray objectAtIndex:someIndex]).valueA

Alternatively, you could simply get rid of the dot syntax altogether (my favourite):

或者,你也可以完全摆脱点语法(我最喜欢的):

[obj2 valueA]
[[someArray objectAtIndex:someIndex] valueA]

#1


7  

The dot syntax is usually applied to declared properties but that’s not mandatory. Using obj.valueA and obj.valueB does work.

点语法通常用于声明属性,但这不是强制的。使用obj。valueA obj。valueB工作。

The error message you’re getting is probably due to the object not having explicit type MyClass *. For example, the following works:

您得到的错误消息可能是由于对象没有显式类型MyClass *。例如:

MyClass *obj1 = [MyClass new];
NSLog(@"%f %f %f", obj1.valueA, obj1.valueB, [obj1 add]);

On the other hand:

另一方面:

MyClass *obj1 = [MyClass new];
NSLog(@"%f %f %f", obj1.valueA, obj1.valueB, [obj1 add]);

id obj2 = obj1;
NSLog(@"%f %f %f", obj2.valueA, obj2.valueB, [obj2 add]);

gives:

给:

error: request for member ‘valueA’ in something not a structure or union
error: request for member ‘valueB’ in something not a structure or union

because obj2 has type id, so the compiler doesn’t have enough information to know that .valueA and .valueB are actually the getter methods -valueA and -valueB. This can happen if you place objects of type MyClass in an NSArray and later retrieve them via -objectAtIndex:, since this method returns a generic object of type id.

因为obj2有类型id,所以编译器没有足够的信息知道。valuea和。valueb实际上是getter方法-valueA和-valueB。如果将MyClass类型的对象放在NSArray中,然后通过-objectAtIndex:检索它们,就会发生这种情况,因为该方法返回类型id的泛型对象。

To appease the compiler, you need to cast the object to MyClass * and only then use the dot syntax. You could accomplish this by:

为了使编译器满意,您需要将对象转换为MyClass *,然后才使用点语法。你可以通过:

MyClass *obj2 = obj1;
// or
MyClass *obj2 = [someArray objectAtIndex:someIndex];
// and then
obj2.valueA

or, if obj2 is declared as id:

或者,如果obj2被声明为id:

((MyClass *)obj2).valueA

or, if the object is returned by a method whose return type is id:

或者,如果对象被返回类型为id的方法返回:

((MyClass *)[someArray objectAtIndex:someIndex]).valueA

Alternatively, you could simply get rid of the dot syntax altogether (my favourite):

或者,你也可以完全摆脱点语法(我最喜欢的):

[obj2 valueA]
[[someArray objectAtIndex:someIndex] valueA]