Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to fix "bad receiver type" error

Tags:

objective-c

I am making an app that allows you to set the name and number of sides of and then the app will automatically calculate the shapes name. I've been asked to change the number of sides from an NSNumber to an int. I have done this but now I get a error saying "bad receiver type". What can I do to correct this but keep the number of side property an int?

This is my header file for the class

#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

This is the implementation file for the class

- (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

This is the Main file

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;
}
like image 215
H41RD Avatar asked Sep 26 '13 12:09

H41RD


1 Answers

The numberOfSides is not an object, but rather a fundamental data type, int, so rather than:

int *numberOfSides;

You really want:

int numberOfSides;

And rather than your if statements like

if ([self.numberOfSides isEqual: @3]) ...

You want

if (self.numberOfSides == 3) ...

And, your calls like:

[myShape setNumberOfSides:@4];

should be:

[myShape setNumberOfSides:4];

(You did this for "3", but not the rest.)

Bottom line, replace all the number literals (which are a NSNumber object) with integers, and replace any isEqual with ==.

like image 91
Rob Avatar answered Nov 08 '22 06:11

Rob