XCode enum help

Joined
Mar 10, 2010
Messages
10
Reaction score
0
Points
1
Location
Burbank, CA
Your Mac's Specs
iMac i5 27" OSX 10.6.2
Hi all,

Not sure if this is the proper place for these questions, but I have gotten great help here in the past and thought I'd try.

I am having a hard time with declaring and using enums in my XCode project.

My declaration is going in the .h file, just above the @implementation line:
Code:
typedef enum {
    EASY,
    NORMAL,
    HARD
} DifficultyLevel

That compiles just fine. But when I try to access it in an instance method within the .m file:
Code:
switch ([self difficultyLevel]) {
    case DifficultyLevel.EASY:
    ...
}

I don't get CodeSense, and get errors. I am obviously not doing this properly, but I cannot find a reference on HOW to do it.

Can someone guide me?

Cheers,

Chris
 
OP
R
Joined
Mar 10, 2010
Messages
10
Reaction score
0
Points
1
Location
Burbank, CA
Your Mac's Specs
iMac i5 27" OSX 10.6.2
Ok, I figured it out, but found another question in the process... isn't that how it always works? :)

The typedef in the header is good.

Using it, though merely requires:
Code:
switch ([self difficultyLevel]) {
    case EASY:
    ...
}

THAT works... but what is the point of giving the typedef a name, such as "DifficultyLevel"?

To me,
Code:
DifficultyLevel.EASY
is much more readable and self-documenting than
Code:
EASY
.

Am I still missing something, or doing it wrong altogether?

Thanks!
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
An enum is not a struct. A struct is a structure that would contain variables that would be referenced with a . notation.

An enum is simply a listing of values something can take on.

in your case, DifficultyLevel can == EASY, but it wouldn't be referenced as DifficutlyLevel.EASY.

Honestly the way it is makes much more sense then what you're trying to convert it to.

Here's an example, let's say you have a switch statement that will do something based upon variable X

switch (x) {
case 1: .... break;
case 2: .... break;
.
.
.
etc.
}

You're testing x to see what it's value is to perform some operation, you're not doing:

case x.1

your testing:

if (x == 1)

it's the same thing with an enum.

you're testing

if (DifficultyLevel == EASY)

Looking at it in the way an if statement would evaluate it sometimes makes it more clear like in this case. EASY is a value that DifficultyLevel can take, EASY is not a part of DifficultyLevel that can be referenced like a variable in a struct.
 
OP
R
Joined
Mar 10, 2010
Messages
10
Reaction score
0
Points
1
Location
Burbank, CA
Your Mac's Specs
iMac i5 27" OSX 10.6.2
Thanks Nethfel,

That helps. I completely forgot about structs... which is how I am looking at it.

But now, what is the purpose of the name of the typedef, as in DifficultyLevel? Would that ever get used?

Cheers,

Chris
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
I honestly never typedef something unless I actually plan to assign another variable to be what I've typedef'd.

Typedef allows you to create a new type of variable identifier. There is no need to use it unless you want to use a unique name to identify a variable type.

In your program there is no need for typedef as you have it written.

Here's an example program that uses typedef that I whipped up real quick to demonstrate:

Code:
#import <Foundation/Foundation.h>

typedef enum {
	EASY,
	NORMAL,
	HARD
} Settings;

int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
	
	Settings SecondSetting = HARD;

	Settings DifficultyLevel = EASY;
	
	switch (DifficultyLevel) {
		case EASY:
			NSLog(@"Easy setting");
			break;
		case NORMAL:
			NSLog(@"Normal Setting");
			break;
		case HARD:
			NSLog(@"Hard Setting");
			break;
		default:
			break;
	}

	switch (SecondSetting) {
		case EASY:
			NSLog(@"Easy setting");
			break;
		case NORMAL:
			NSLog(@"Normal Setting");
			break;
		case HARD:
			NSLog(@"Hard Setting");
			break;
			
			
		default:
			break;
	}
	
	
	
    [pool drain];
    return 0;
}

And the output it generates is:

2010-06-10 14:04:28.868 typtest[3353:a0f] Easy setting
2010-06-10 14:04:28.873 typtest[3353:a0f] Hard Setting

As you can see - I have typedef'd the enumeration to a key word Settings. I then defined two variables (DifficultyLevel and SecondSetting) to be of type Settings (and assigned them a respective value)

Using typedef is convenient if you don't want to have to retype something like this enum or a struct over and over again.

Here's a good description of the purpose of typedef along with more samples: Typedef - Wikipedia, the free encyclopedia

I should note - I am leaving out a variable name in one place - in the enum I could have done:

typedef enum Settings {
EASY,
NORMAL,
HARD
} Difficulties

then in the program

Difficulties difficultyLevel;
.
.
.
difficultyLevel = EASY;

etc.
 
OP
R
Joined
Mar 10, 2010
Messages
10
Reaction score
0
Points
1
Location
Burbank, CA
Your Mac's Specs
iMac i5 27" OSX 10.6.2
Ok, making more sense.

I see I do not need the typedef, but then how would I create the enum for use by the class as a whole?

Should I just declare it as an assigned variable in the header like:
Code:
@interface ...
    int aVar;
    bool anotherVar;
    enum {
        EASY,
        NORMAL,
        HARD
    }
...

Is that the proper way to do it?
 
Joined
Feb 25, 2009
Messages
2,112
Reaction score
71
Points
48
Your Mac's Specs
Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10
You can do it that way - it'd probably be cleaner to create it outside of the class and typedef it so if you were to use the enum outside of the class, or within another class you could without having to recreate it - plus it would probably look cleaner in the class itself to have

Difficulties difficultyLevel

rather then having the enum in there.
 

Shop Amazon


Shop for your Apple, Mac, iPhone and other computer products on Amazon.
We are a participant in the Amazon Services LLC Associates Program, an affiliate program designed to provide a means for us to earn fees by linking to Amazon and affiliated sites.
Top