views:

908

answers:

3

What is the standard way of incorporating helper/utility functions in Obj-C classes?

I.e. General purpose functions which are used throughout the application and called by more than 1 class.

Can an Obj-C method exist outside of a class, or does it need to be a C function for it to have this kind of behaviour?

+3  A: 

I would group similar functions as static methods in a helper class. These can then be called using the classname rather the instance name. Static methods are defined with a + instead of the usual -.

like so:

@interface HelperClass: superclassname {
    // instance variables - none if all methods are static.
}

+ (void) helperMethod: (int) parameter_varName;

@end

This would be called like so.

[HelperClass helperMethod: 10 ];

As this is static you do not init/alloc the class. Hope this helps.

Tony

Tony Lambert
Class names should usually start with an upper case (see http://google-styleguide.googlecode.com/svn/trunk/objcguide.xml for a good style guide)
Michel
+3  A: 

There are a number of options for this in Objective-C. First, since Obj-C is a strict superset of C, you can define all your library functions in a separate module (source file) and happily call them from any Obj-C object/code you already have. If you create an obj-c source file (.m file) you can then call back into/use objects.

If your generic functions are logically manipulating other, established objects (for instances, operates on an NSString), you can use categories to graph your functions on already existing classes (where that makes sense).

Finally, as Tony points out, you can create classes with static methods (although I like this option the least, personally). I tend to use a mix of one an two... adding categories where appropriate and using standard functions for others. I generally only make a new class where it makes sense to design a class.

Jason Coco
If I create a .m file, don't I still have to create my methods inside a class implementation? Is there a way for Obj-C methods to be defined outside of a class implementation and be easily included as class methods?
firstresponder
I created a category for NSObject. This is working well for me atm, however it does not seem entirely right to me that my helper functions should be exposed to every single class in my application. The category is in NSObject because it needs to be accessed in my app delegate and other classes.
firstresponder
For things that have no real class or category, you can use standard C functions. They can still use objects and references inside.
Jason Coco
+5  A: 

I don't see why people are avoiding creating functions. Objective-C is a superset of C, which means that C is part of it. Moreover, it's completely integrated—there's no wall between them.

Create functions! It's fine! Foundation does it. Application Kit does it. Core Animation does it. QuickTime Kit does it.

I see no reason not to.

Peter Hosey
Exactly! I see no reason either.
Jason Coco