For constant, I prefer to use a .h file like this
// ConstanteDef.h
For now, I'm using Singleton to return a more complex element to me.
Here is one single I made to simplify my life with basic data, instead of rewriting the same code everywhere.
@interface CoreDataController : NSObject { NSManagedObjectContext *leManagedObjectContext; NSManagedObjectModel *leManagedObjectModel; @private Commerce_MO *leCommerceAucun; } @property (nonatomic, retain, readonly) NSManagedObjectContext *managedObjectContext; @property (nonatomic, retain, readonly) NSManagedObjectModel *managedObjectModel; #pragma mark Objet par Défaut @property (nonatomic, retain, readonly) Commerce_MO *commerceAucun; #pragma mark Nouvel Objet // new = retain count = 1, celui qui commande est responsable de la mémoire. - (id)newMOforClass:(Class)uneClasse; // Pas le mieux, mais pourrais servir pendant le run time. Retourne nil si uneClasse ne correspond pas à quelque chose. - (PrixElement_MO *)newPrixElement; - (ItemInfos_MO *)newItemInfos; - (Commerce_MO *)newCommerce; - (Liste_MO *)newListe; - (ListeItem_MO *)newListeItem; #pragma mark Singleton call + (CoreDataController *)sharedCoreDataController; @end
So, in my code, when I need to create a new object, I just need to do this:
CoreDataController *cdc = [CoreDataController sharedCoreDataController]; Liste_MO * = [cdc newListe];
For more information on the Singleton concept, see HERE in the Apple documentation in the Creating a Singleton Instance section and carefully look at the code that they represent giving a singleton that should answer your request for a weak or strong link to it.
But in essence, a strict singleton implementation will have only one instance of this class, created for the entire duration of the application. Therefore, if you have 100 objects pointing to it, this does not change the print of your memory, there is only one singleton, but if you have 100 objects that will definitely affect your memory.
Vincent bierier
source share