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?

2
  • Possible duplicate stackoverflow.com/questions/707512/… Commented Jun 5, 2013 at 15:45
  • @voromax it can be related, maybe even duplicated, but not an exact duplicated... Commented Jun 5, 2013 at 21:12

2 Answers 2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.