I want to make a file that contains useful functions for my project. Well, I know that I can define a function inside my class implementation. But I don't really get it... I mean: I don't want to create a "class", just functions. Or are functions always part of a class? My objective-c knowledge is pretty limited, and my nice big book on objective-c does mention them, but does not explain how to implement them stand-alone.
+4
A:
Organize your code in plain old C style:
- a header file (.h) containing all function declarations.
- a body file (.c) containing all function bodies.
Include your header file wherever you need one of your functions.
Let Xcode manage compilation and link.
mouviciel
2009-05-20 13:10:12
How would a header declaration of a function look like? Just the same way as a method declaration?
Thanks
2009-05-20 14:40:49
The syntax is a bit different but the principle is similar. for instance, the declaration of fopen function is: FILE *fopen(const char *FILE, const char *MODE);
mouviciel
2009-05-20 15:08:29
+4
A:
Because Objective-C is a superset of C, you can do this in the normal c way, in that you define your functions in a header file, which you include wherever you want to use them, then implement them in the .c file.
I'd probably implement them as class methods on a class in order to namespace them, and prevent any accidental collisions, though.
Mr. Matt
2009-05-20 13:16:39