Objective c 指针、指向二进制的无效操作数和noob

Objective c 指针、指向二进制的无效操作数和noob,objective-c,binary,operands,Objective C,Binary,Operands,[编辑:在底部添加矩形定义。] [Edit2:XYPoint接口添加在底部。] 我正在研究一种检查两个矩形是否重叠的方法。(是的,我在Kochan的Objective-C编程中做练习,对此我非常陌生。)当我编译这个时,错误消息是:“二进制+的操作数无效”。我在第一个if语句和后面的if-else语句中得到它 我想我对指针有点意见,但科昌并没有说太多 如果我去掉这些行,剩下的方法就可以了。且相关变量均为浮动型 帮忙 此外,对该方法的任何其他想法都是完全受欢迎的。(比如,我怎样才能让代码行不那么长。

[编辑:在底部添加矩形定义。] [Edit2:XYPoint接口添加在底部。]

我正在研究一种检查两个矩形是否重叠的方法。(是的,我在Kochan的Objective-C编程中做练习,对此我非常陌生。)当我编译这个时,错误消息是:“二进制+的操作数无效”。我在第一个if语句和后面的if-else语句中得到它

我想我对指针有点意见,但科昌并没有说太多

如果我去掉这些行,剩下的方法就可以了。且相关变量均为浮动型

帮忙

此外,对该方法的任何其他想法都是完全受欢迎的。(比如,我怎样才能让代码行不那么长。就像我说的,这是一个令人痛苦的新发现。)

XYPoint接口:

#import <Foundation/Foundation.h>

@interface XYPoint : NSObject
{
    float x;
    float y;
}

@property float x, y;

-(void) setX: (float) xVal andY: (float) yVal;

@end
#导入
@接口XYPoint:NSObject
{
浮动x;
浮动y;
}
@财产浮动x,y;
-(无效)setX:(浮动)xVal安迪:(浮动)伊瓦尔;
@结束

您刚刚得到的可能是一个打字错误:

// Test to see if the Rectangle contains, or is equal to, 
// Rectangle b

if (origin.x <= r2.origin.x && origin.y <= r2.origin.y && 
   (origin.x + width) >= (r2.origin + r2.width) && 
                         //^^^This is trying to add an XYPoint,
                         // which is an object, to a float.
   (origin.y + height) >= (r2.origin.y + r2.height) ) 
{
   overlapRectangle = r2;
}

// Test to see if Rectangle b contains, or is equal to, 
// the Rectangle
else if (origin.x >= r2.origin.x && origin.y >= r2.origin.y && 
         origin.x + width <= r2.origin + r2.width && 
                            //^^^Same thing.
         origin.y + height <= r2.origin.y + r2.height ) 
{
...
你马上就会知道发生了什么。以下是一些建议:

- (BOOL)containsRectangle:(Rectangle *)otherRect {
    BOOL originBelow = ((origin.x <= otherRect.origin.x) && 
                        (origin.y <= otherRect.origin.y));
    float maxX = origin.x + width;
    float otherMaxX = otherRect.origin.x + otherRect.width;
    BOOL maxXGreater = maxX >= otherMaxX;
    Bfloat maxY = origin.y + height;
    float otherMaxY = otherRect.origin.y + otherRect.height;
    BOOL maxYGreater = maxY >= otherMaxY;
    return originBelow && maxXGreater && maxYGreater;
}

- (BOOL)isEqualToRectangle:(Rectangle *)otherRect {
    BOOL sizeEqual = ((width == otherRect.width) && 
                      (height == otherRect.height));
    return sizeEqual && [origin isEqualToXYPoint:otherRect.origin];

}
-(BOOL)包含矩形:(矩形*)其他矩形{
BOOL origindown=((origin.x=otherMaxY;
返回originbelower&&maxgreater&&maxYGreater;
}
-(BOOL)相等矩形:(矩形*)其他矩形{
BOOL sizeEqual=((宽度==其他矩形宽度)和
(高度==其他直线高度));
返回sizeEqual&[origin isEqualToXYPoint:otherRect.origin];
}
注意:我没有测试这些,只是根据
if
s的条件将它们粘贴在一起,所以在使用它们之前请仔细检查。不过,我确实修复了拼写错误


请注意,我在这里也在
XYPoint
上创建了一个方法,
isEqualToXYPoint:
;如果
XYPoint
x
y
相等,您也可以实现它,返回一个
BOOL

在哪一行报告了错误?您可以发布矩形的定义吗?有什么特别的原因吗n不使用CGRect和CGRectIntersectsRect(CGRect rect1、CGRect rect2)从CGGeometry?您的
宽度
高度
,以及原点坐标的类型是基本类型,如
浮动
?或对象?编辑:哦,您将发布标题。这将回答我的问题。太好了!好了,现在
XYPoint
的界面是什么?:)回复:
CGRectIntersectsRect
——这是一个很好的问题,但这些函数只适用于苹果的
CGRect
结构。它们不能用于这里的自定义类。好吧,我想我知道错在哪里了。正在研究一个答案……乔希,它起作用了。很抱歉在打字错误上遇到了这么多麻烦。我应该已经抓住了。你建议的其他内容gested也很有道理。如果我知道任何有关NSXMLParser错误代码的信息,我会尝试帮助您解答问题,但我认为这可能需要一段时间。再次感谢!无需道歉;我很乐意帮助您!祝您好运!(从中您可以记住一件事,当您的代码行如此长且复杂时,很难发现拼写错误——尝试将其分解以使其更易于阅读。)
// Test to see if the Rectangle contains, or is equal to, 
// Rectangle b

if (origin.x <= r2.origin.x && origin.y <= r2.origin.y && 
   (origin.x + width) >= (r2.origin + r2.width) && 
                         //^^^This is trying to add an XYPoint,
                         // which is an object, to a float.
   (origin.y + height) >= (r2.origin.y + r2.height) ) 
{
   overlapRectangle = r2;
}

// Test to see if Rectangle b contains, or is equal to, 
// the Rectangle
else if (origin.x >= r2.origin.x && origin.y >= r2.origin.y && 
         origin.x + width <= r2.origin + r2.width && 
                            //^^^Same thing.
         origin.y + height <= r2.origin.y + r2.height ) 
{
...
if( [self containsRectangle:r2] || [self isEqualToRectangle:r2] ){
- (BOOL)containsRectangle:(Rectangle *)otherRect {
    BOOL originBelow = ((origin.x <= otherRect.origin.x) && 
                        (origin.y <= otherRect.origin.y));
    float maxX = origin.x + width;
    float otherMaxX = otherRect.origin.x + otherRect.width;
    BOOL maxXGreater = maxX >= otherMaxX;
    Bfloat maxY = origin.y + height;
    float otherMaxY = otherRect.origin.y + otherRect.height;
    BOOL maxYGreater = maxY >= otherMaxY;
    return originBelow && maxXGreater && maxYGreater;
}

- (BOOL)isEqualToRectangle:(Rectangle *)otherRect {
    BOOL sizeEqual = ((width == otherRect.width) && 
                      (height == otherRect.height));
    return sizeEqual && [origin isEqualToXYPoint:otherRect.origin];

}