Ryan
Ryan

Reputation: 7957

Where would I place a global utility function in Objective C?

When writing an iOS app, where would I place a function that I intend to use from any other file?

For example, a function to convert a NSDate to a relative time string ("5 secs ago"). Would I make a class and make these functions all static?

Upvotes: 19

Views: 13076

Answers (4)

Jog
Jog

Reputation: 225

Another option would be to create a class for your helper methods and implement all the helpers as class methods.

e.g. HelperClass.h

+ (NSString *)getFrenchCapital

e.g. HelperClass.m

+ (NSString *)getFrenchCapital
{
    return @"Paris";
}

Then import your helper class wherever you need it, and simply call the class methods:

e.g. Foo.m

#import "HelperClass.h"

...

- (void)logFrenchCapital
{
    NSLog(@"Capital of France: %@", [HelperClass getFrenchCapital]);
} 

Upvotes: 6

JRG-Developer
JRG-Developer

Reputation: 12663

You have a couple options:

1) If you're extending the behavior of a class (such as the NSDate string conversion method you described), it may work best to simply create a category on said class.

Here's a tutorial on iOS categories:

http://mobile.tutsplus.com/tutorials/iphone/objective-c-categories/

Important Note:

Categories change a class's behavior (if you override a method) everywhere within the project whether or not you include the header (.h) file in another specific class's imports

For this reason, it's generally best to not override methods via a category, but instead, to create a subclass if you want to change certain methods.

For adding new methods, however, categories can be very convenient and useful.

2) If you want to create a new class that's imported everywhere, you can create said class and put its header import, i.e. #import "MyClass.h", into your project's prefix.pch file (found under the "supporting files" group within the project by default).

Anything that you put into the prefix.pch file will be available anywhere within your app. This is also a useful place to put constants (such as strings) or define enums that are used across many classes within the app.

I hope this helps. Let me know if further clarification is needed, and I'll do my best to help.

Cheers!

Upvotes: 11

jlehr
jlehr

Reputation: 15617

Functions can be placed wherever convenient. If a function or group of functions is likely to be imported in many places, you can declare and implement them in their own .h/.m pair. So for example you might implement your date conversion function in a file named XYZDateUtilities.m, and declare it in XYZDateUtilities.h.

Declaring functions with the static qualifier would limit their scope to the file in which they were declared, so you wouldn't want to do that; in fact you'd want to do the opposite -- declare them as extern in the .h file so that they'll be visible in other files.

Upvotes: 18

TieDad
TieDad

Reputation: 9939

If you make all functions static in a class, then alternative is to just define functions in .m file, and extern functions in .h file, just like what you do in C.

Upvotes: 0

Related Questions