views:

213

answers:

1

I have some Objective-C code that looks like this:

#define myVar 10
float f = 10.0;
- (void) myFunc{ ... }

#define anotherVar 20
float z = 30.0;
- (void) myFunc2{ ... }

// and so on

It's all in one class. I'd like to put the first block of code into another file and just reference it somehow. So the above code would end up looking something like this:

// some reference to the code... #import?  #include?  I don't know...

#define anotherVar 20
float z = 30.0;
- (void) myFunc2{ ... }

How can I do this? I just want the most basic, simple, and crude solution. Cheers!

+2  A: 

You can do it with an include. For example:

File1.m

#define myVar 10
float f = 10.0;
- (void) myFunc{ ... }

File2.m

#include "File1.m"

#define anotherVar 20
float z = 30.0;
- (void) myFunc2{ ... }

Just make sure you remove File1.m from the build because it won't compile by itself.

Tom Dalling
Well, File1.m *will* compile by itself. It's just that when the linker comes to link them together, File2.o will have all the symbols in File1.o, so they'll conflict. But either way, removing File1.m is the right move.
BJ Homer