I’ve searched all over for an answer to this simple question, with no luck. So here it is: Is it possible to access an Object’s structure values in Objective-C without using dot notation, i.e., using only the canonical message syntax? E.g., let’s say that we have a CGPoint instance variable, position, defined for a Particle object:
//Particle.h
@interface Particle : NSObject {
CGPoint position
}
@property CGPoint position;
@end
and the corresponding implementation:
//Particle.m
#import "Particle.h"
@implementation Particle
@synthesize position;
@end
And suppose that in some other *.m file (say AppController.m) we have:
a_particle = [Particle alloc];
[a_particle setPosition:CGPointMake(x_val,y_val)];
Then why can’t we use:
[[a_particle position] x];
to get x_val rather than:
a_paticle.position.x;
noting that the latter works fine? Note also that
[a_particle position].x;
will work, but I am trying to avoid cluttering things up with dot notation. My hunch is that a structure instance variable MUST use dot notation to access its values (in this case x_val and y_val), is this true?
A
CGPointis just a plain old Cstruct, not an objective-c class so you can’t send messages to it. You need to use C’s syntax for reading struct members, ie the ‘.’