New To Mac-Forums?

Welcome to our community! Join the discussion today by registering your FREE account. If you have any problems with the registration process, please contact us!

Get your questions answered by community gurus Advice and insight from world-class Apple enthusiasts Exclusive access to members-only contests, giveaways and deals

Join today!

 
Start a Discussion
 

Mac-Forums Brief

Subscribe to Mac-Forums Brief to receive special offers from Mac-Forums partners and sponsors

Join the conversation RSS
OS X - Development and Darwin Discussion and questions about development for Mac OS X.

XCode enum help


Post Reply New Thread Subscribe

 
Thread Tools
Raconteur

 
Member Since: Mar 10, 2010
Location: Burbank, CA
Posts: 10
Raconteur is on a distinguished road
Mac Specs: iMac i5 27" OSX 10.6.2

Raconteur is offline
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
QUOTE Thanks
Raconteur

 
Member Since: Mar 10, 2010
Location: Burbank, CA
Posts: 10
Raconteur is on a distinguished road
Mac Specs: iMac i5 27" OSX 10.6.2

Raconteur is offline
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!
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,109
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
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.

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks
Raconteur

 
Member Since: Mar 10, 2010
Location: Burbank, CA
Posts: 10
Raconteur is on a distinguished road
Mac Specs: iMac i5 27" OSX 10.6.2

Raconteur is offline
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
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,109
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
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:

Quote:

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.

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks
Raconteur

 
Member Since: Mar 10, 2010
Location: Burbank, CA
Posts: 10
Raconteur is on a distinguished road
Mac Specs: iMac i5 27" OSX 10.6.2

Raconteur is offline
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?
QUOTE Thanks
Nethfel

 
Member Since: Feb 25, 2009
Posts: 2,109
Nethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of lightNethfel is a glorious beacon of light
Mac Specs: Late 2013 rMBP, i7, 750m gpu, OSX versions 10.9.3, 10.10

Nethfel is offline
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.

My Macs: Late 2013 rMBP w/ 750m; Mac mini G4, 1.25 GHz, 512m ram (server); Late 2011 11" MBA, 1.8GHz i7, 4Gig Ram, 256Gig SSD, HD3000; Powerbook 12" G4 1.33GHz running Debian as a server; Apple TV (1080p version)
QUOTE Thanks

Post Reply New Thread Subscribe


« recommended xCode and cocoa development guides | Where is the allocation file? »
Thread Tools

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts
BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off
Forum Jump

Similar Threads
Thread
Thread Starter
Forum
Replies
Last Post
content.yieldmanager.edgesuite.net XCODE nachohi88 Internet, Networking, and Wireless 1 03-27-2010 10:41 PM
Xcode, Subversion, and directory behaviour Reinderien OS X - Development and Darwin 1 03-10-2009 12:50 PM
Carbon framework on XCode Rafi Shalom OS X - Development and Darwin 1 04-17-2008 02:14 PM
Xcode 1.5 alejandrico OS X - Development and Darwin 0 06-03-2006 06:56 PM
New Xcode plug-in helps developers reuse code Murlyn OS X - Development and Darwin 0 11-10-2003 01:16 PM

All times are GMT -4. The time now is 05:09 AM.

Powered by vBulletin
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.
X

Welcome to Mac-Forums.com

Create your username to jump into the discussion!

New members like you have made this community the ultimate source for your Mac since 2003!


(4 digit year)

Already a member?