I have an iOS app that uses a number of enums for valid values, mostly as I’m using some integer-driven state transition tables, but also allows lower memory usage and the ability to index-search in SQLite, which I am using for the backend.
I’ve got all the enums in a common .h file, which is included across a number of classes that create variables based on the enum type. I also have several methods which are used across many classes. Some of them are translating the enums back to strings, with the typedef and a (human) language as paramaters.
One of the typedefs is below:
typedef enum {
Ben,
Sen,
O,
Biz,
Siz,
Onlar
} pronoun_typedef;
There are also another set of methods, mostly string manipulation which are fed several parameters and return a NSString* or NSMutableString*.
I’ve already moved these around to different classes a few times, and I am thinking the best approach might be to create a “utility” class and include it in each relevant class, and instantiate as required.
Is this a good approach? The other option I considered would be to create a singleton, and then forward reference it to every class where it would be required. Any suggestions on which approach to use, or any suggestions for other approaches?
I know this is a similar question to this and some others but I didn’t feel it was conclusively answered so I thought I ask again. Singleton and Static Utility classes
If the utility methods don’t need a reference to self, I would make them C functions defined in the same module as the constants. You need an .h file with the constant definitions and function declarations (prototypes), and a .c file with function bodies defined.
If it needs to be an object, use a singleton if single instance makes sense, and multiple instances if each one needs a private, independent copy of its internal data/state.