iPhone – Using some symbols as an example method

I looked at a piece of code today and noticed that this particular encoder uses dot notation to access instance methods (these methods do not accept values, they only return values).

For example:

@interface MyClass: NSObject {

}
-(double)add;
@end

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

-(double)valueB {< br /> return 7.0;
}

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

He did this through his code and the compiler did not complain, but when I tried it in my code, as in the above example, I got the following error: "Request member "valueA" in a certain In the structure or unity of things". What am I missing?

dot syntax is usually applied to declared attributes, but this is not required. Use obj.valueA and obj.valueB does work.

The error message you get may be because the object does not have an explicit type MyClass *. For example, the following works:

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

Get:

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 a type id, so the compiler does not have enough information to know that .valueA and .valueB are actually getter methods -valueA and -valueB. If you put an object of type MyClass in an NSArray, and then pass -objectAtIndex: to retrieve them, then This happens because this method returns a generic object of type id.

To appease the compiler, you need to cast the object to MyClass* before using dot syntax. You can do this in the following way Complete

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

Or, if obj2 is declared as id:

((MyClass *)obj2).valueA 

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

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

Or, you can get rid of dot syntax altogether (my favorite):

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

I saw a piece of code today and noticed that this particular encoder uses dot notation to access instance methods (these methods do not accept values, they only return values ).

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 his code and the compiler did not complain, but when I tried it in my code, like the example above I got the following error: "Request The member "valueA" is in some structure or union. What am I missing?

Dot syntax is usually applied to declared attributes, but this is not required. Using obj.valueA and obj.valueB does work.

The error message you get may be because the object does not have an explicit type MyClass *. For example, the following works:

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

Get:

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 a type id, the compiler does not have enough information Knowing that .valueA and .valueB are actually getter methods -valueA and -valueB. If you put an object of type MyClass in an NSArray, and then retrieve them via -objectAtIndex:, this will happen because this method returns a type The universal object of id.

To appease the compiler, you need to cast the object to MyClass* before using dot syntax. You can do it in the following ways

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

Or, if obj2 is declared as id:

((MyClass *)obj2).valueA

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

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

Or, you can get rid of the dot syntax altogether (my favorite):

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

Leave a Comment

Your email address will not be published.