Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

andywhitt

macrumors member
Original poster
May 15, 2006
41
0
UK
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 :)
 
Last edited:

ulbador

macrumors 68000
Feb 11, 2010
1,554
0
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
 

andywhitt

macrumors member
Original poster
May 15, 2006
41
0
UK
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.
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

andywhitt

macrumors member
Original poster
May 15, 2006
41
0
UK
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 :)
 

PhoneyDeveloper

macrumors 68040
Sep 2, 2008
3,114
93
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.
 

andywhitt

macrumors member
Original poster
May 15, 2006
41
0
UK
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
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.