point
の値が変化するたびに の値も変化すると著者は書いていますが、オブジェクトに変換するとmyRect's origin
どのように修正されるのかわかりません。origin
オブジェクトとポインターの作成の違いは何ですか?
固定setOrigin
方法:
-(void) setOrigin:(XYpoint *)pt {
if (! origin)
origin = [[XYpoint alloc] init];
origin.x = pt.x;
origin.y = pt.y;
}
#import <Foundation/Foundation.h>
@interface XYPoint : NSObject
@property int x, y;
@end
#import "XYpoint.h"
@implementation XYPoint
@synthesize x, y;
@end
#import <Foundation/Foundation.h>
@class XYPoint;
@interface Rectangle: NSObject
-(void) setOrigin: (XYPoint *) pt;
@end
#import "Rectangle.h"
#import "XYpoint.h"
@implementation Rectangle {
XYPoint *origin;
}
-(void) setOrigin:(XYPoint *)pt {
origin = pt;
}
@end
#import "XYpoint.h"
#import "Rectangle.h"
int main (int argc, char *argv[]) {
@autoreleasepool {
Rectangle *myRect = [[Rectangle alloc] init];
XYpoint *point = [[XYpoint alloc] init];
point.x = 3;
point.y = 8;
[myRect setOrigin: point];
}
return 0;
}