我正在制作一个允许您设置名称和边数的应用程序,然后该应用程序将自动计算形状名称。我被要求将边数从 an 更改NSNumber
为 an int
。我已经这样做了,但现在我收到一条错误消息,说“接收器类型错误”。我能做些什么来纠正这个问题,但要保持边属性的数量int
?
这是我的类头文件
#import <Foundation/Foundation.h>
@interface shape : NSObject
@property int *numberOfSides;
@property (nonatomic, strong) NSString *name;
@property (nonatomic, strong) NSString *colour;
@property (nonatomic, strong) NSString *calculatedName;
void waitOnCR (void);
- (NSString *)calculatedName;
@end
这是该类的实现文件
- (NSString *)calculatedName {
if ([self.numberOfSides isEqual: @3]) {
return self.name = @"Triangle";
}
else if([self.numberOfSides isEqual: @4]) {
return self.name = @"Square";
}
else if([self.numberOfSides isEqual: @5]) {
return self.name = @"Pentagon";
}
else if([self.numberOfSides isEqual: @6]) {
return self.name = @"Hexagon";
}
else if([self.numberOfSides isEqual: @7]) {
return self.name = @"Heptagon";
}
else if([self.numberOfSides isEqual: @8]) {
return self.name = @"Octagon";
}
return [NSString stringWithFormat:@"%@", self.name ];
}
@end
这是主文件
NSLog(@"Enter a number from between 3-8");
int user;
scanf("%d" , &user);
switch (user) {
case 3:
{
shape *myShape = [[shape alloc]init];
[myShape setNumberOfSides:3];
[myShape setColour:@"Red"];
id s2 = [myShape calculatedName];
NSLog(@"The %@ shape has %i sides and is called a %@", [myShape colour], [myShape numberOfSides], s2);
break;
}
case 4:
{
{
shape *myShape = [[shape alloc]init];
[myShape setNumberOfSides:@4];
[myShape setColour:@"Blue"];
id s2 = [myShape calculatedName];
NSLog(@"The %@ shape has %@ sides and is called a %@", [myShape colour], [myShape numberOfSides], s2);
break;
}
case 5:
{
shape *myShape = [[shape alloc]init];
[myShape setNumberOfSides:@5];
[myShape setColour:@"Orange"];
id s2 = [myShape calculatedName];
NSLog(@"The %@ shape has %@ sides and is called a %@", [myShape colour], [myShape numberOfSides], s2);
break;
}
case 6:
{
shape *myShape = [[shape alloc]init];
[myShape setNumberOfSides:@6];
[myShape setColour:@"Purple"];
id s2 = [myShape calculatedName];
NSLog(@"The %@ shape has %@ sides and is called a %@", [myShape colour], [myShape numberOfSides], s2);
break;
}
case 7:
{
shape *myShape = [[shape alloc]init];
[myShape setNumberOfSides:@7];
[myShape setColour:@"Green"];
id s2 = [myShape calculatedName];
NSLog(@"The %@ shape has %@ sides and is called a %@", [myShape colour], [myShape numberOfSides], s2);
break;
}
case 8:
{
shape *myShape = [[shape alloc]init];
[myShape setNumberOfSides:@8];
[myShape setColour:@"Pink"];
id s2 = [myShape calculatedName];
NSLog(@"The %@ shape has %@ sides and is called a %@", [myShape colour], [myShape numberOfSides], s2);
break;
}
}
default:
NSLog(@"Shape not found!");
break;
}