Sto provando a lavorare su un tutorial Objective-C. Nel libro c'è questo esempio:Objectetter-C getter/setter
@interface
{
int width;
int height;
XYPoint *origin;
}
@property int width, height;
ho pensato: ". Hey non c'è getter/setter per l'oggetto Xypoint Il codice funziona anche se" Ora sto andando forse a rispondere alla mia stessa domanda :).
I pensa la sua causa "origine" è un puntatore già, e che cosa sta accadendo sotto il cofano con "larghezza" e "altezza", è che ci sta te creare un puntatore a loro ..
Am ho ragione, o sto parlando BS :) ??
Non ho capito. ecco principale:
#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;
}
Ed ecco l'oggetto Rectangle:
#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
Quello che non capisco è questa linea principale: myRect.origin = myPoint;
non ho fatto un setter per esso ..
BTW, grazie per di