views:

203

answers:

3

I want to create a set of functions, that make life easier. I use them in about 60% of all project files. I wonder what's the most efficient way of including these in the project.

I think about creating one file where they go in and including this file in every file where I need them. On the other hand, it would be cool if I can include that just once in a prominent place, so that's available throughout the whole project. But I don't know what place that could be. I'm not sure, but I think that this is not possible. Or am I wrong with that assumption?

+1  A: 

You can include your headers in ProjectName_Prefix.pch, I think.

Mr. Matt
+3  A: 

The prefix.pch file that you have in your xcode project is used adding all import statements that you'll need in all/almost all classes of your project.

The UIKit and Foundation imports are written in this file.

So just add your import statement to this file and you're done. You'll find it in the Other Sources folder in Groups & Files

lostInTransit
+6  A: 

I think that you are asking more about class design and how you structure your APIs rather than how you get the definitions included into your other .m files.

If that is the case, there are roughly three options:

  • Add something to your app delegate - this is a good place to store system wide functions that contro lthe overall model or the app itself.
  • create a signleton class to hold your methods. Either an actual singleton or a class with just class methods (i.e. declared with a leading + rather than a -)
  • by adding a category onto a system class.

I think that in general you do not want to make a single decision for all your utility methods - you should take each method (or grouop of methods) on a case by case basis and do the right thing. For example if you have a method that displays your own style of error dialog, you could put this on the AppDelegate. If you have a method to translate a string into French you could add that as an NSString category.

In general, categories are your friend here - they can be encapsulated in a single .h and .m so that they are easy to move around projects and yet their accessibility follows the target classes.

Failing that you should look at implementing a singleton. See Matt Gallager's discussion on how to do that.

Finally, if it really is the right decision, extend your AppDelegate - possibly with a category ;-)

Roger Nolan