3

Below is the enum I created in the header file of my Ball class:

typedef enum   {
redBall = 0,
blueBall = 1,
greenBall = 2

}ballTypes;

and in the interface:

ballTypes ballType;

in the init method of Ball.mm I initialized ballType as follows:

ballType = 0;

I get the following error:

Assigning to 'ballTypes' from incompatible type 'int'

How can I solve this?

IMustCode
  • 109
  • 8

2 Answers2

3

Enums should be defined with the NS_ENUM macro:

typedef NS_ENUM(NSInteger, BallType) {
    BallTypeNone  = 0,
    BallTypeRed   = 1,
    BallTypeBlue  = 2,
    BallTypeGreen = 3
};

BallType ballType;

ballType = BallTypeNone;

Typically the name starts with a capital letter and each value is the name with a meaningful description appended to it.

jszumski
  • 7,430
  • 11
  • 40
  • 53
  • Even if it solves the problem and introduce a good practice (thanks btw, I didn't know about NS_ENUM), the _real_ problem of the question is better adressed by trojanfoe's answer imho. – Guillaume Algis Jun 05 '13 at 15:49
1

BallTypes is a type and int (literal 0) is a type and they cannot be mixed without casting.

Create an invalid ball type and use that:

typedef enum   {
    noBall,
    redBall,
    blueBall,
    greenBall
} ballTypes;

...

ballType = noBall;

Note: Conventionally enums are capitialized...

trojanfoe
  • 120,358
  • 21
  • 212
  • 242