views:

69

answers:

2

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
How would a header declaration of a function look like? Just the same way as a method declaration?
Thanks
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
+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
When they're part of a class, then other objects don't have to instantiate that class in order to use the functions, right?
Thanks
If you implement them as class methods, then no. Just do this: [MyClass somefunction];
Mr. Matt