Using dot notation like methods

I looked at a piece of code today and notice that this particular encoder uses dot notation to access instance methods (these methods don't take a value, they just return a value).

For instance:

@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 his code, and the compiler did not complain, but when I try to use it in my code, as in the above example, I get the following error: "Request for member" valueA "in something that is not a structure or association. " What am I missing, any idea?

+3
source share
1 answer

, . obj.valueA obj.valueB .

, , , , MyClass *. , :

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

:

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]);

:

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

obj2 id, , , .valueA .valueB getter -valueA -valueB. , MyClass NSArray -objectAtIndex:, id.

, MyClass * . :

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

, obj2 id:

((MyClass *)obj2).valueA

, , id:

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

, ( ):

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

All Articles