#import <Foundation/Foundation.h>
typedef enum{
kRedColor,
kGreenColor,
kBlueColor
}ShapeColor;
//enum
typedef struct{
int x,y,width,height;
}ShapeRect;
//struct
void drawShapes(id shapes[] ,int count);
NSString *colorName(ShapeColor color)
{
NSString *colorName;
switch (color)
{
case kRedColor:
colorName = @"red";
break;
case kBlueColor:
colorName = @"blue";
break;
case kGreenColor:
colorName = @"green";
break;
}
return(colorName);
}//colorName
@interface Shape : NSObject
{
ShapeColor fillColor;
ShapeRect bounds;
}
-(void) setFillColor:(ShapeColor) fillColor;
-(void) setBounds:(ShapeRect) bounds;
-(void) draw;
@end//superclass
@implementation Shape
- (void) setFillColor: (ShapeColor) c
{
fillColor = c ;
}
- (void) setBounds: (ShapeRect) b
{
bounds=b;
}
- (void) draw
{
}
@end
@interface Triangle: Shape
@end
@implementation Triangle
-(void) draw
{
NSLog(@" drawing a triangle at (%d %d %d %d ) in %@ ",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));
}
@end//triangle
@interface Circle:Shape
@end
@implementation Circle
-(void) draw
{
NSLog(@" drawing a circle at (%d %d %d %d ) in %@",bounds.x,bounds.y,bounds.width,bounds.height,colorName(fillColor));
}
@end //circle
void drawShapes(id shapes[] ,int count)
{
for ( int i=0 ;i<count;i++)
{
id shape =shapes[i];
[shape draw];
}
}//drawshapes
int main(int argc , const char*argv[])
{
id shapes[2];
ShapeRect rect0={0,0,10,30};
shapes[0]=[Circle new];
[shapes[0] setBounds:rect0];
[shapes[0] setFillColor:kRedColor];
ShapeRect rect1={30,40,50,60};
shapes[1]=[Triangle new];
[shapes[1] setBounds:rect1];
[shapes[1] setFillColor:kGreenColor];
drawShapes(shapes,2);
return (0);
}//main
- 错误的翻译:必须显式地描述目标对象的所有权。大概就是分配 空间的问题、不符合内存管理的规则 。
处理办法:处理办法就是将设置项目 Automatic Reference Counting 变为No,因为你Xcode4.2以上的带有这个设置并默认设置YES。这个设置主要是设置自动内存管理。我们将其设为不自动管理就不报错了
ARC forbids explicit message send of 'retainCount'
解决步骤:
打开当前工程,打开"Build Settings",找到Objective-C Automatic Reference Counting项,将它的值设置为NO。