我不明白使用!運算符在if(!origin)中。作者解釋說,它正在測試實例變量的來源,看它的值是否非零,但我不太明白這意味着什麼。說明!關係運算符
#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
-(XYPoint *) origin;
-(void) setOrigin: (XYPoint *) pt;
@end
#import "Rectangle.h"
#import "XYpoint.h"
@implementation Rectangle {
XYPoint *origin;
}
-(void) setOrigin:(XYPoint *)pt {
if (! origin)
origin = [[XYpoint alloc] init];
origin.x = pt.x;
origin.y = pt.y
}
-(XYPoint *) origin {
return origin;
}
@end