Objective c “如何修复”;“接收器类型错误”;错误
我正在制作一个应用程序,它允许你设置形状的名称和边数,然后应用程序将自动计算形状名称。我被要求将边数从Objective c “如何修复”;“接收器类型错误”;错误,objective-c,Objective C,我正在制作一个应用程序,它允许你设置形状的名称和边数,然后应用程序将自动计算形状名称。我被要求将边数从NSNumber更改为int。我已经这样做了,但现在我得到一个错误,说“坏接收器类型”。如何更正此问题,但要将边属性的编号保持为int 这是我的类头文件 #import <Foundation/Foundation.h> @interface shape : NSObject @property int *numberOfSides; @property (nonatomic,
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;
}
numberOfSides
不是一个对象,而是一种基本数据类型,int
,因此不是:
int *numberOfSides;
你真的想要:
int numberOfSides;
而不是你的if
语句
if ([self.numberOfSides isEqual: @3]) ...
你想要
if (self.numberOfSides == 3) ...
你的电话是:
[myShape setNumberOfSides:@4];
应该是:
[myShape setNumberOfSides:4];
(你这样做是为了“3”,而不是其他。)
总之,用整数替换所有数字文本(这是一个NSNumber
对象),并用=
替换任何isEqual
,闻起来像是一个赋值(理论上这很好,但这里的问题很基本。)我投票决定结束>常见问题解答:“询问代码的问题必须表明对正在解决的问题的最低理解。”