views:

751

answers:

5

Hi, im trying to works my way trough an Objective-C tutorial. In the book there is this example:

@interface
{
 int width;
 int height;
 XYPoint *origin;
}
@property int width, height;

I though, hey there's no getter/setter for the XYPoint object. The code does work though. Now i'm going maybe to answer my own question :).

I thinks its because "origin" is a pointer already, and whats happening under the hood with "width" and "height", is that there is going te be created a pointer to them..

Am i right, or am i talking BS :) ??

I just dont get it here's main:

#import "Rectangle.h"
#import "XYPoint.h"
int main (int argc, char *argv[])
{
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];

    Rectangle *myRect = [[Rectangle alloc] init];
    XYPoint *myPoint = [[XYPoint alloc] init];

    [myPoint setX: 100 andY: 200];
    [myRect setWidth: 5 andHeight: 8];

    myRect.origin = myPoint; 

    NSLog (@"Rectangle w = %i, h = %i",
           myRect.width, myRect.height); 

    NSLog (@"Origin at (%i, %i)",
           myRect.origin.x, myRect.origin.y);

    NSLog (@"Area = %i, Perimeter = %i",
           [myRect area], [myRect perimeter]);

    [myRect release];
    [myPoint release];
    [pool drain];

    return 0;
}

And here's the Rectangle object:

#import "Rectangle.h"
#import "XYPoint.h"

@implementation Rectangle
@synthesize width, height;

-(void) setWidth: (int) w andHeight: (int) h
{
    width = w;
    height = h;
}

- (void) setOrigin: (XYPoint *) pt
{
    origin = pt;
}
-(int) area
{
    return width * height;
}
-(int) perimeter
{
    return (width + height) * 2;
}
-(XYPoint *) origin
{
    return origin;
}
@end

What i dont understand is this line in main: myRect.origin = myPoint; I did not make a setter for it..

BTW thanks for your fast reply's

A: 

You can think of a property declaration as being equivalent to declaring two accessor methods. Thus

@property int width;

is equivalent to:

- (int)width;

- (void)setWidth:(int)newWidth;
Schildmeijer
A: 

You don't say what code is working, or what your expectations are for "working".

The above interface will create simple accessor methods for width and height that can be called from other objects as [object setWidth:1]; or object.width = 1; - these two are analogous.

Origin is some other object type and is a pointer, yes. But you would still want to declare a property for it to generate accessor methods.

Paul Lynch
A: 

Getters and setters are mostly useful if you need to access an instance variable from another class or you're using bindings to get/set them. So my guess would be that you need this functionality for the width and height but not for the origin. Note that the getters/setters do not make pointers out of the integers as you stated might be the reason. Ints are ints and getters/setters do not change that.

regulus6633
+1  A: 

What i dont understand is this line in main: myRect.origin = myPoint; I did not make a setter for it..

There is both a getter and a setter (collectively referred to as accessors) created for origin in the Rectangle class. If you have a look in the implementation for Rectangle, this is the getter:

-(XYPoint *) origin
{
    return origin;
}

and this is the setter:

- (void) setOrigin: (XYPoint *) pt
{
    origin = pt;
}

And as of Objective-C 2.0 calling:

myRect.origin = myPoint;

is equivalent to:

[myRect setOrigin:myPoint];

Declaring getters and setters using @property (and then implementing them using @synthesize) is only one way of declaring and creating accessors, and is there for a convenience if you have lots of properties to declare in the class interface. As Schildmeijer said, @property int width is equivalent to declaring two methods:

- (int)width;
- (void)setWidth:(int)newWidth;

Due to the dynamically-bound nature of Objective-C method calls, you don't even have to declare the getter and setter methods in the interface, although it is generally best practice to do so if you are advertising them as publicly available to other classes.

Perspx
A: 

For me, being a newcomer, the dot notation is confusing. I think i like the bracket notation.

pic-o-matic