20

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?

Jason Coco
  • 77,985
  • 20
  • 184
  • 180
firstresponder
  • 5,000
  • 8
  • 32
  • 38

3 Answers3

27

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. This has the advantage of clearly grouping like Helper functions. You could use standalone C functions but as your Application gets larger it can become a right mess! Hope this helps.

Tony

AnthonyLambert
  • 8,768
  • 4
  • 37
  • 72
  • 2
    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 Dec 15 '08 at 09:29
  • 1
    Also, method names should start with a lowercase letter, e.g., helperMethod. – Raffi Khatchadourian Jan 03 '12 at 17:59
  • Style is just that.... you can choose your own unique one if you wish... Otherwise it would be called a law.... In the spirit of not breaking "the style" I have changed it. ;-) – AnthonyLambert Nov 17 '15 at 15:10
20

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. Core Media does it.

I see no reason not to.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
  • Exactly! I see no reason either. – Jason Coco Dec 15 '08 at 17:45
  • I guessing you have never worked on a large C development? I think the static method in a class is far more tidy. Imagine an App with 100's of functions in 100's of files. Using a class groups them together and helps those that come after you understand the code. – AnthonyLambert Jan 04 '12 at 11:26
  • 3
    @AnthonyLambert: I'm not opposed to class methods when they're appropriate. It's certainly possible to have too many functions, but it's also possible to have too many class methods. If they belong together, they should be in the same class. If you're creating class after class for one or two methods each or lumping unrelated methods into a vaguely-related (or, worse, vaguely-named) class, just make them functions. (Warning sign: A class named “MyFileUtilities” with no instance methods. Just make them functions.) – Peter Hosey Jan 04 '12 at 12:14
7

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
  • 77,985
  • 20
  • 184
  • 180
  • 1
    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 Dec 15 '08 at 10:24
  • 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 Dec 15 '08 at 11:27
  • 2
    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 Dec 15 '08 at 17:45