Note that there are some explanatory texts on larger screens.

plurals
  1. POObjective C - Accessors ie Getters/Setters
    primarykey
    data
    text
    <p>I Have read the already posted questions extensively and can't quite find the answer I'm looking for. </p> <p>I fully understand the concept of using the <code>@syntesize</code> directive to create getter and setter methods (i.e. if i had <code>@property int width</code> and <code>@synthesize width</code>, I am inadvertently creating a getter method of <code>width</code> and a setter method of <code>setWidth:</code>). </p> <p>However, when I am not using the <code>@synthesize</code> directive but am declaring instance variables in the <code>@implementation</code> section that are objects, I do not fully understand how the accessor methods work. This is what I do not understand about the following code:</p> <p>1) in <code>main</code> where it says: </p> <pre><code>NSLog(@"Origin at (%i, %i)", myRect.origin1.x, myRect.origin1.y); </code></pre> <p>It appears to me as if it would be calling the <code>[[myRect origin1] x]</code> method which would first determine that <code>[myRect origin1]</code> returns <code>origin</code> and would then immediately call <code>[origin x]</code> as a result (and then do the same for <code>y</code>). Now, what throws me off is the fact that if I were to change the name of the getter method</p> <pre><code>-(XYpoint *) origin1; </code></pre> <p>contained within Rectangle.h to</p> <pre><code>-(XYpoint *) origin2; </code></pre> <p>the program gets tons of errors and ceases to compile. Note: I also changed the name of this method everywhere it is referenced including changing the preceding code in main to</p> <pre><code>NSLog(@"Origin at (%i, %i)", myRect.origin2.x, myRect.origin2.y); </code></pre> <p>However if I also change the name of the setter method from:</p> <pre><code>-(void) setOrigin1: (XYpoint *) pt </code></pre> <p>to:</p> <pre><code>-(void) setOrigin2: (XYpoint *) pt </code></pre> <p>then everything works as it did before. It seems to me it only works correctly when my getter and setter are both named in the <code>x</code> <code>setX</code> naming convention. I supposed this is mainly what I need explained:</p> <p>A) If I create an instance variable that happens to be an object (like 'origin' in this case) must I create getter and setter methods for it?</p> <p>B) Can I create a getter method but not a setter method or vice versa</p> <p>C) Is it mandatory that if I do create both a getter and setter method for 'origin' that they both be named in the <code>x</code> <code>setX</code> manner. In this case as <code>-(XYpoint *) origin1</code> and <code>-(void) setOrigin1: (XYpoint *) pt</code>. As in if I change the name of the getter I must change the name of the setter accordingly?</p> <p>Here is all of the code:</p> <p>Rectangle.h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @class XYpoint; @interface Rectangle : NSObject @property int width, height; -(XYpoint *) origin1; -(void) setOrigin1: (XYpoint *) pt; -(void) setWidth: (int) w andHeight: (int) h; -(int) area; -(int) perimeter; @end </code></pre> <p>Rectangle.m:</p> <pre><code>#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 </code></pre> <p>XYpoint.h:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; @interface XYpoint : NSObject @property int x, y; -(void) setX: (int) xVal andY: (int) yVal; @end </code></pre> <p>XYpoint.m:</p> <pre><code>#import "XYpoint.h" @implementation XYpoint @synthesize x,y; -(void) setX: (int) xVal andY: (int) yVal { x = xVal; y = yVal; } @end </code></pre> <p>main.m:</p> <pre><code>#import &lt;Foundation/Foundation.h&gt; #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; } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload