Results 1 to 7 of 7

Thread: XCode enum help

  1. #1


    Member Since
    Mar 10, 2010
    Location
    Burbank, CA
    Posts
    10
    Specs:
    iMac i5 27" OSX 10.6.2
    XCode enum help
    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

  2. #2


    Member Since
    Mar 10, 2010
    Location
    Burbank, CA
    Posts
    10
    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!

  3. #3


    Member Since
    Feb 25, 2009
    Posts
    2,112
    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.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; 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 Linux; Apple TV (1080p version)

  4. #4


    Member Since
    Mar 10, 2010
    Location
    Burbank, CA
    Posts
    10
    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

  5. #5


    Member Since
    Feb 25, 2009
    Posts
    2,112
    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.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; 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 Linux; Apple TV (1080p version)

  6. #6


    Member Since
    Mar 10, 2010
    Location
    Burbank, CA
    Posts
    10
    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?

  7. #7


    Member Since
    Feb 25, 2009
    Posts
    2,112
    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.
    My Macs: Late 2013 rMBP w/ 750m, 16Gig ram; 2013 Mac Pro 6 core w/ D700, 16Gig Ram; 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 Linux; Apple TV (1080p version)

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Xcode stopped letting me run Xcode app on iPad
    By ildsarria in forum iOS Development
    Replies: 1
    Last Post: 11-09-2011, 04:15 PM
  2. C++ AI in Xcode
    By raydot in forum OS X - Development and Darwin
    Replies: 0
    Last Post: 03-27-2008, 01:12 AM
  3. xcode
    By hal515 in forum OS X - Apps and Games
    Replies: 0
    Last Post: 11-15-2007, 10:44 PM
  4. using xcode
    By shimanok2 in forum OS X - Apps and Games
    Replies: 2
    Last Post: 06-25-2007, 08:22 PM
  5. where is xcode?
    By macman88 in forum OS X - Operating System
    Replies: 5
    Last Post: 05-03-2005, 11:58 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •