I have read the questions already posted widely and cannot find the answer I'm looking for.
I fully understand the concept of using the @syntesize directive to create getter and setter methods (i.e. if I had @property int width and @synthesize width , I inadvertently create the getter width method and setter setWidth: method).
However, when I do not use the @synthesize directive, but declare instance variables in the @implementation section, which are objects, I donβt quite understand how access methods work. Here is what I do not understand about the following code:
1) in main , where it says:
NSLog(@"Origin at (%i, %i)", myRect.origin1.x, myRect.origin1.y);
It seems to me that he will call the method [[myRect origin1] x] , which first determines that [myRect origin1] returns origin , and then immediately calls [origin x] as a result (and then does the same for y ). Now I'm sick of the fact that if I changed the name of the getter method
-(XYpoint *) origin1;
contained in Rectangle.h until
-(XYpoint *) origin2;
the program receives a lot of errors and stops compiling. Note. I also changed the name of this method everywhere it refers, including changing the previous code in main to
NSLog(@"Origin at (%i, %i)", myRect.origin2.x, myRect.origin2.y);
However, if I also change the name of the setter method from:
-(void) setOrigin1: (XYpoint *) pt
in
-(void) setOrigin2: (XYpoint *) pt
then everything works as before. It seems to me that it only works correctly when my getter and setter are both named in the x setX naming setX . I suggested that this is basically what I need to explain:
A) If I create an instance variable that is an object (for example, "origin" in this case), should I create getter and setter methods for it?
B) Can I create a getter method, but not a setter method or vice versa
C) Is it mandatory if I create a getter and setter method for "origin", which both will be named in the style of x setX . In this case, both -(XYpoint *) origin1 and -(void) setOrigin1: (XYpoint *) pt . As in the case when I change the name of the recipient, should I change the name of the setter accordingly?
Here is the whole code:
Rectangle.h:
Rectangle.m:
#import "Rectangle.h" @implementation Rectangle { XYpoint *origin; } @synthesize width, height; -(void) setWidth:(int) w andHeight:(int)h { width = w; height = h; } -(void) setOrigin1: (XYpoint *) pt { origin = pt; } -(int) area { return width * height; } -(int) perimeter { return (width + height) * 2; } -(XYpoint *) origin1 { return origin; } @end
XYpoint.h:
XYpoint.m:
main.m:
#import <Foundation/Foundation.h> #import "Rectangle.h" #import "XYpoint.h" int main (int argc, const char * argv[]) { @autoreleasepool { Rectangle *myRect = [[Rectangle alloc] init]; XYpoint *myPoint = [[XYpoint alloc] init]; [myPoint setX: 100 andY: 200]; [myRect setWidth: 5 andHeight:8]; myRect.origin1 = myPoint; NSLog(@"Rectangle w = %i, h = %i", myRect.width, myRect.height); NSLog(@"Origin at (%i, %i)", myRect.origin1.x, myRect.origin1.y); NSLog(@"Area = %i, Perimeter = %i", [myRect area], [myRect perimeter]); } return 0; }