这个项目是在xcode中运行的,当运行时总是无法成功,提示for循环的问题(i等于一个负数)!代码如下,有问题的代码用红字标出,请各位帮忙看一下,谢谢了!
#import <Foundation/Foundation.h>
typedef enum{
kCircle,
kRectangle,
kEgg
}ShapeType;
typedef enum{
kRedColor,
kGreenColor,
kBlueColor
}ShapeColor;
typedef struct{
int x,y,width,height;
}ShapeRect;
typedef struct{
ShapeType type;
ShapeColor fillColor;
ShapeRect bounds;
}shape;
//返回颜色的值
NSString *colorName(ShapeColor fillColor){
switch(fillColor){
case kRedColor:
return (@”red”);
break;
case kGreenColor:
return (@”Green”);
break;
case kBlueColor:
return (@”Blue”);
break;
}
}
//声明Circle
@interface Circle:NSObject{
@private
ShapeRect bounds;
ShapeColor fillColor;
}
-(void)setBounds:(ShapeRect) bounds;
-(void)setColor:(ShapeColor) fillColor;
-(void)draw;
@end
//声明Rectangle
@interface Rectangle:NSObject{
@private
ShapeRect bounds;
ShapeColor fillColor;
}
-(void)setBounds:(ShapeRect) bounds;
-(void)setColor:(ShapeColor) fillColor;
-(void)draw;
@end
//声明Egg
@interface Egg:NSObject{
@private
ShapeRect bounds;
ShapeColor fillColor;
}
-(void)setBounds:(ShapeRect) bounds;
-(void)setColor:(ShapeColor) fillColor;
-(void)draw;
@end
//实现Circle
@implementation Circle
-(void)setBounds:(ShapeRect)b{
bounds=b;
}
-(void)setColor:(ShapeColor)c{
fillColor=c;
}
-(void)draw{
NSLog(@”\ndrawing a circle at(%d,%d,%d,%d) in %@\n”,
bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));
}
@end
//实现Rectangle
@implementation Rectangle
-(void)setBounds:(ShapeRect)b{
bounds=b;
}
-(void)setColor:(ShapeColor)c{
fillColor=c;
}
-(void)draw{
NSLog(@”\ndrawing a Rectangle at(%d,%d,%d,%d) in %@\n”,
bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));
}
@end
//实现Egg
@implementation Egg
-(void)setBounds:(ShapeRect)b{
bounds=b;
}
-(void)setColor:(ShapeColor)c{
fillColor=c;
}
-(void)draw{
NSLog(@”\ndrawing a Egg at(%d,%d,%d,%d) in %@\n”,
bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));
}
@end
//实例化对象
void drawShapes(id shapes[],int count){
for(int i=0;i<count;i++){
id shape=shapes[i];
[shape draw];
}
}
int main(int agrc,const char *argv[]){
id shapes[3];
ShapeRect bounds0={0,0,10,30};
shapes[0]=[Circle new];
[shapes[0] setBounds:bounds0];
[shapes[0] setColor:kRedColor];
ShapeRect bounds1={30,40,50,60};
shapes[1]=[Rectangle new];
[shapes[1] setBounds:bounds1];
[shapes[1] setColor:kGreenColor];
ShapeRect bounds2={15,18,37,29};
shapes[2]=[Egg new];
[shapes[2] setBounds:bounds2];
[shapes[2] setColor:kBlueColor];
drawShapes(shapes, 3);
return (0);
}