2
我正在制作一个应用程序,允许您设置名称和边数,然后应用程序将自动计算形状名称。我被要求将NSNumber
的边数改为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;
}
气味像一些任务(理论上这样会很好,但这里的问题是基本的......即时通讯投票结束>常见问题解答:“询问代码的问题必须证明对所解决问题的最小理解。 –