![](/img/trans.png)
[英]Is there a way to define the setter and getter method for internal properties in Objective-C?
[英]Objective-C Setter Method not working (simple)
我一輩子都無法弄清楚為什么myRect.origin.x和myRect.origin.y顯示的值為0。對代碼的廣度表示歉意,我確實刪除了一些顯然不相關但不確定的東西在其他方面。 另外,我知道我的相交方法尚不能正常工作,但我正在研究它。
//Rectangle.h
#import <Foundation/Foundation.h>
@class XYPoint;
@interface Rectangle: GraphicObject
@property float height, width;
-(XYPoint*) origin;
-(void) setOrigin: (XYPoint*) pt;
-(float) area;
-(float) perimeter;
-(void) setHeight: (float) h andWidth: (float) w;
-(void) translate: (XYPoint*) t;
-(BOOL) containspoint: (XYPoint*) aPoint;
-(Rectangle*) intersect: (Rectangle*) intr;
@end
@interface XYPoint: NSObject
@property float x,y;
-(void) setX:(float) xVal andY:(float) yVal;
@end
//Rectangle.m
#import "Rectangle.h"
@implementation Rectangle
{
XYPoint *origin;
}
@synthesize width, height;
-(float) area
{
return width*height;
}
-(float) perimeter
{
return 2*(width*height);
}
-(void) setHeight:(float) h andWidth: (float) w
{
width = w;
height = h;
}
-(void) setOrigin:(XYPoint *)pt
{
origin = pt;
}
-(XYPoint*) origin
{
return origin;
}
-(void) translate:(XYPoint *)t
{
origin = t;
}
-(BOOL) containspoint:(XYPoint *)aPoint
{
if (aPoint.x >= self.origin.x && aPoint.x <= self.origin.x + self.width && aPoint.y >= self.origin.y && aPoint.y <= self.origin.y + self.height) {
return YES;
}
else return NO;
}
-(Rectangle*) intersect: (Rectangle*) intr
{
Rectangle *zerorec = [[Rectangle alloc]init];
Rectangle *rec1 = [[Rectangle alloc]init];
Rectangle *rec2 = [[Rectangle alloc]init];
zerorec.origin.x = 0;
zerorec.origin.y = 0;
zerorec.height = 0;
zerorec.width = 0;
if ((self.origin.x >= intr.origin.x && self.origin.x <= intr.origin.x + intr.width) || (intr.origin.x >= self.origin.x && intr.origin.x >= self.origin.x + self.width))
{
if ((self.origin.y >= intr.origin.y && self.origin.y <= intr.origin.y + intr.height) || (intr.origin.y >= self.origin.y && intr.origin.y >= self.origin.y + self.height))
NSLog(@"The rectangles intersect");
//rec 1 = the rectangle with the greater x value
if (self.origin.x >= intr.origin.x && self.origin.x <= intr.origin.x + intr.width)
rec1 = self, rec1.origin = self.origin, rec2 = intr, rec2.origin = intr.origin;
else rec2 = self, rec2.origin = self.origin, rec1 = intr, rec1.origin = intr.origin;
Rectangle *returnedRec = [[Rectangle alloc]init];
returnedRec.origin.x = rec1.origin.x;
returnedRec.origin.y = rec2.origin.y;
//rec 1 = rectangle with greater y value
if (self.origin.y > intr.origin.y)
rec1 = self, rec2 = intr;
else rec1 = intr, rec2 = self;
returnedRec.height = rec1.height - (rec1.origin.y - rec2.origin.y);
returnedRec.width = rec1.width - (rec2.origin.y - rec1.origin.y);
return returnedRec;
}
else return zerorec;
}
@end
@implementation XYPoint
@synthesize x, y;
-(void) setX:(float) xVal andY:(float) yVal
{
x = xVal;
y = yVal;
}
@end
//main.m
#import <Foundation/Foundation.h>
#import "Rectangle.h"
int main(int argc, const char * argv[])
{
@autoreleasepool {
Rectangle *returnedRect = [[Rectangle alloc]init];
Rectangle *myRect = [[Rectangle alloc]init];
Rectangle *yourRect = [[Rectangle alloc]init];
[myRect.origin setX:3 andY: 2];
[myRect setHeight:4 andWidth:5];
[yourRect.origin setX:6 andY:4];
[yourRect setHeight:7 andWidth:9];
myRect.origin.x = 2;
myRect.origin.y = 3;
returnedRect = [myRect intersect:yourRect];
NSLog(@"x = %f, y = %f", returnedRect.origin.x, returnedRect.origin.y);
NSLog(@"height = %f, width = %f", returnedRect.height, returnedRect.width);
NSLog(@"%f, %f", myRect.origin.x, myRect.origin.y);
}
return 0;
}
如您所見,我嘗試使用setX和Y方法以及顯式設置x和y值,並且程序仍然為myRect.origin的x和y值返回值0。
您的原點始終為零,因為您從未創建XYPoint對象。 將此添加到您的Rectangle類:
- (id)init
{
self = [super init];
if (self) {
origin = [[XYPoint alloc] init];
}
return self;
}
似乎您從未創建XYPoint
實例,因此您向nil
發送了許多消息。
您應該使用CGRect
(即使您的類是它的包裝器),因為它會使您的代碼和工作變得更加簡單。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.