Enum Question

Discussion in 'iOS Programming' started by andywhitt, Feb 24, 2011.

  1. andywhitt, Feb 24, 2011
    Last edited: Feb 24, 2011

    andywhitt macrumors member

    andywhitt

    Joined:
    May 15, 2006
    Location:
    UK
    #1
    Hi, I'm very new to Objective-C and the Cocoa framework. I'm primarily a C# .NET Devleoper, and I have a problem with enums in Objective-C hopefully one of you can answer.

    Code:
    #import <Foundation/Foundation.h>
    
    enum SomeEnum{
    	None	 = 0,
    	OptionOne = 1,
    	OptionTwo = 2
    };
    typedef int SomeEnum;
    
    @interface untitled : NSObject {
    }
    -(void) doSomethingBasedOn: (int) id;
    @end
    
    @implementation untitled
    -(void) doSomethingBasedOn: (int) id{
    	switch(id)
    	{
    		case SomeEnum.None:
    			break;
    		case SomeEnum.OptionOne:
    			break;
    		case SomeEnum.OptionTwo:
    			break;
    			
    	}
    }
    @end
    

    When I build this I get a build error "Expected expression before 'SomeEnum'" on all 3 of the cases. This is valid in C# and Java, I wondered what I am doing wrong in Objective-C?

    Thanks :)
     
  2. ulbador macrumors 68000

    ulbador

    Joined:
    Feb 11, 2010
    #2
    This works for me with just a few changes. I've never actually used enums in Objective C, so I'm kinda flying by the seat of my pants.


    Code:
    #import <Foundation/Foundation.h>
    
    typedef enum{
    	None	 = 0,
    	OptionOne = 1,
    	OptionTwo = 2
    } SomeEnum;
    
    
    @interface untitled : NSObject {
    }
    -(void) doSomethingBasedOn: (SomeEnum) id;
    @end
    
    @implementation untitled
    -(void) doSomethingBasedOn: (SomeEnum) id{
    	switch(id)
    	{
    		case None:
    			NSLog(@"None");
    			break;
    		case OptionOne:
    			NSLog(@"One");
    			break;
    		case OptionTwo:
    			break;
    			
    	}
    }
    @end
    
     
  3. andywhitt thread starter macrumors member

    andywhitt

    Joined:
    May 15, 2006
    Location:
    UK
    #3
    Thanks for the reply, but its not quite what I was thinking, I need the parameter passed to doSomethingBasedOn as an int, In C# you'd do this:
    Code:
     enum SomeEnum
        {
            None = 0,
            OptionOne = 1,
            OptionTwo = 2
        }
    
        class Program
        {
            void DomeSomeThing(int id)
            {
                var enumthing = (SomeEnum) id;
                switch (enumthing)
                {
                    case SomeEnum.None:
                        break;
                    case SomeEnum.OptionOne:
                        break;
                    case SomeEnum.OptionTwo:
                        break;
                }
            }
        }
    
    Guess the step I'm missing is getting the Enum value of my int.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    typedef generates a new type that is recognized by the compiler and even the debugger. typedef enum makes a named enum that is a new type. It isn't a class. It's a type, like int or float. SomeEnum.OptionOne is a Java-ism and not valid C. You just refer to a member of the enum by its name, like OptionOne.

    Doing it the way that ulbador shows is superior to the way that OP shows. For some reason Apple has a lot of typedefed enums done like OP shows. If you do it the way that ulbador shows then the debugger will show your values with their enum names, rather than just as integers. That can be very helpful when debugging. I think that Apple does it the other way to guarantee that the enums are the full size of an int, but it screws up the debugger display, and there's no reason to require that.
     
  5. andywhitt thread starter macrumors member

    andywhitt

    Joined:
    May 15, 2006
    Location:
    UK
    #5
    Thanks for the reply, I've got it to compile and changed to ulbador's method.

    Code:
    #import <Foundation/Foundation.h>
    
    typedef enum{
    	None	 = 0,
    	OptionOne = 1,
    	OptionTwo = 2
    } SomeEnum;
    
    @interface untitled : NSObject {
    }
    -(void) doSomethingBasedOn: (int) id;
    
    @end
    
    @implementation untitled
    -(void) doSomethingBasedOn: (int) id{
    	switch(id)
    	{
    		case None:
    			break;
    		case OptionOne:
    			break;
    		case OptionTwo:
    			break;
    			
    	}
    }
    @end
    
    Is this how you would do it tho? doesn't quite look right to me :)
     
  6. huskerchad macrumors regular

    Joined:
    Jul 17, 2002
    #6
    I wouldn't name a variable "id" in an objective-C program.
     
  7. andywhitt thread starter macrumors member

    andywhitt

    Joined:
    May 15, 2006
    Location:
    UK
    #7
    Yeah sorry, bad example.
     
  8. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #8
    I'm unclear on why you want a named type and then not use it. Why don't you pass the values as SomeEnum? Why do you use int?

    enum is the C way to create constants, as well as a way to generate new types. If you just want to create integer constants it makes more sense to use an anonymous enum.

    Code:
    enum {
    	None	 = 0,
    	OptionOne = 1,
    	OptionTwo = 2
    };
    
    and then use the constants in your code as you show. You can also use #define to make integer constants.
     
  9. andywhitt thread starter macrumors member

    andywhitt

    Joined:
    May 15, 2006
    Location:
    UK
    #9
    The reason I use int as a method parameter is that's what I get from an external source.

    I also use these enums internally else where, and if one was to change, I only have to do it in one place,

    Code:
    enum {
    	None	 = 0,
    	OptionOne = 1,
    	OptionTwo = 4 // note change
    };
    Thanks
     

Share This Page