Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 343
Ryan Avatar asked Dec 29 '12 04:12

Ryan


1 Answers

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.

like image 107
jlehr Avatar answered Nov 11 '22 18:11

jlehr