PDA

View Full Version : The use of enum




blue-lion
Dec 1, 2010, 04:16 PM
Hello, as practice i want to write a simple program to use the enum command. However, i don't know where to define/declare the enum types? I want to use them throughout the program and i will be passing them as parameters to classes (and perhaps returning ), so the interface section will have to recognize them and even be able to assign them to an instance variable?.

If i define them the main program ,will they have scope in all objects and derived classes?



kainjow
Dec 1, 2010, 04:22 PM
Put them in a separate header file, and include that header wherever you want to use them.

blue-lion
Dec 1, 2010, 05:02 PM
okay, i never thought of that.Thanks

However, how do i achieve that?

Does it go with the instance variables in header file?, it seems the structure of the objective c headers is quite rigid. Where in the file would i put them?. Also do i leave the implementation file empty? , or just no implementation file at all?

You can probably tell i am a newbie to objective c.....

lloyddean
Dec 1, 2010, 05:49 PM
Anywhere before they are otherwise referenced will do.

On the other hand header files should NOT contain instance variables.

ianray
Dec 2, 2010, 02:48 AM
it seems the structure of the objective c headers is quite rigid.

That's not quite true.

Header files typically contain things which one wants to share:

C enums, data structures, function prototypes etc
C++ classes etc
Objective-C interfaces, protocols etc


Consider these two examples:


// foo.c
#include <stdio.h>

enum
{
red,
green,
blue
};

int main(void)
{
printf("red: %d, green: %d, blue: %d\n", red, green, blue);
return 0;
}


foo.c and bar.c produce the same program when compiled:


// bar.h
enum
{
red,
green,
blue
};



// bar.c
#include <stdio.h>

#include "bar.h"

int main(void)
{
printf("red: %d, green: %d, blue: %d\n", red, green, blue);
return 0;
}


Hope that helps :)

blue-lion
Dec 2, 2010, 02:53 AM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5)

That's a good point actually. There are no variables in header files?? If I have a header purely for enums do I have always have to have some kind of implementation file to accompany it. I could really use an example. Know of any?

saltyzoo
Dec 2, 2010, 03:16 AM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5)

That's a good point actually. There are no variables in header files?? If I have a header purely for enums do I have always have to have some kind of implementation file to accompany it. I could really use an example. Know of any?

Yeah, there's a really clear, simple one in the post above yours. ;)

gnasher729
Dec 2, 2010, 06:17 AM
That's a good point actually. There are no variables in header files?? If I have a header purely for enums do I have always have to have some kind of implementation file to accompany it. I could really use an example. Know of any?

For an enum, or for a typedef, or for #define statements, there is nothing to implement. So if your header file contains nothing else, then there is no need for an implementation file.

In C++, in rare cases you will have classes where everything is implemented as inline functions; in that case you don't need an implementation file either.

Objective-C classes always need an implementation, so you would always have an implementation file.

Catfish_Man
Dec 2, 2010, 01:35 PM
Wirelessly posted (Mozilla/5.0 (iPhone; U; CPU iPhone OS 4_2_1 like Mac OS X; en-us) AppleWebKit/533.17.9 (KHTML, like Gecko) Version/5.0.2 Mobile/8C148a Safari/6533.18.5)

That's a good point actually. There are no variables in header files?? If I have a header purely for enums do I have always have to have some kind of implementation file to accompany it. I could really use an example. Know of any?

The structure of these files is not enforced in any way. #import is literally equivalent to copy-pasting one file into another. You can do everything in the .h, or nothing in the .h, or not have a .h. You can put variables anywhere.