Register FAQ / Rules Forum Spy Search Today's Posts Mark Forums Read
Go Back   MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Reply
 
Thread Tools Search this Thread Display Modes
Old Dec 1, 2010, 04:16 PM   #1
blue-lion
macrumors member
 
Join Date: Jan 2010
The use of enum

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?
blue-lion is offline   0 Reply With Quote
Old Dec 1, 2010, 04:22 PM   #2
kainjow
Moderator emeritus
 
kainjow's Avatar
 
Join Date: Jun 2000
Put them in a separate header file, and include that header wherever you want to use them.
kainjow is offline   0 Reply With Quote
Old Dec 1, 2010, 05:02 PM   #3
blue-lion
Thread Starter
macrumors member
 
Join Date: Jan 2010
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.....
blue-lion is offline   0 Reply With Quote
Old Dec 1, 2010, 05:49 PM   #4
lloyddean
macrumors 6502a
 
Join Date: May 2009
Location: Des Moines, WA
Anywhere before they are otherwise referenced will do.

On the other hand header files should NOT contain instance variables.
lloyddean is offline   0 Reply With Quote
Old Dec 2, 2010, 02:48 AM   #5
ianray
macrumors 6502
 
Join Date: Jun 2010
Location: @
Quote:
Originally Posted by blue-lion View Post
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
__________________
My App Store Apps: Dashometer, Exinf, Score&Grade
ianray is offline   0 Reply With Quote
Old Dec 2, 2010, 02:53 AM   #6
blue-lion
Thread Starter
macrumors member
 
Join Date: Jan 2010
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?
blue-lion is offline   0 Reply With Quote
Old Dec 2, 2010, 03:16 AM   #7
saltyzoo
macrumors 65816
 
saltyzoo's Avatar
 
Join Date: Oct 2007
Quote:
Originally Posted by blue-lion View Post
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.
saltyzoo is offline   0 Reply With Quote
Old Dec 2, 2010, 06:17 AM   #8
gnasher729
macrumors G5
 
gnasher729's Avatar
 
Join Date: Nov 2005
Quote:
Originally Posted by blue-lion View Post
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.
gnasher729 is offline   0 Reply With Quote
Old Dec 2, 2010, 01:35 PM   #9
Catfish_Man
macrumors 68030
 
Catfish_Man's Avatar
 
Join Date: Sep 2001
Location: Portland, OR
Send a message via AIM to Catfish_Man
Quote:
Originally Posted by blue-lion View Post
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.
Catfish_Man is offline   0 Reply With Quote

Reply
MacRumors Forums > Apple Systems and Services > Programming > Mac Programming

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Similar Threads
thread Thread Starter Forum Replies Last Post
Resolved: Const v typedef enum v #Define charmofmaking iPhone/iPad Programming 19 Jun 6, 2013 12:30 AM
Structs and Enum problems nashyo iPhone/iPad Programming 5 Sep 9, 2012 01:19 PM

Forum Jump

All times are GMT -5. The time now is 02:00 AM.

Mac Rumors | Mac | iPhone | iPhone Game Reviews | iPhone Apps

Mobile Version | Fixed | Fluid | Fluid HD
Copyright 2002-2013, MacRumors.com, LLC