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

blue-lion

macrumors member
Original poster
Jan 26, 2010
50
0
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

Moderator emeritus
Jun 15, 2000
7,958
7
Put them in a separate header file, and include that header wherever you want to use them.
 

blue-lion

macrumors member
Original poster
Jan 26, 2010
50
0
enum's

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.....
 

ianray

macrumors 6502
Jun 22, 2010
452
0
@
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:

Code:
// 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:

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

Code:
// 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

macrumors member
Original poster
Jan 26, 2010
50
0
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

macrumors 65816
Oct 4, 2007
1,065
0
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

Suspended
Nov 25, 2005
17,980
5,565
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

macrumors 68030
Sep 13, 2001
2,579
2
Portland, OR
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.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.