Dead easy singletons in Obj-C
Simply declare your class in your header file like so:
@interface MyClass : NSObject + (RecordingsMgr*)instance; @end
Then in your .m file, declare the instance function like so:
@implementation MyClass
+ (MyClass*)instance {
static MyClass* _myClassInstance = nil;
if (!_myClassInstance) {
_myClassInstance = [[MyClass alloc] init];
}
return _myClassInstance;
}
@endThen, whenever you need the instance somewhere, get it like so:
[[MyClass instance] someInstanceMethod]
Beautifully simple, and no mucking around with app delegates, it’ll simply get created whenever first needed. Not thread safe, however, so stick to the main thread.
Update
If you’re concerned about thread safety, you can use this technique:
http://cocoasamurai.blogspot.com/2011/04/singletons-your-doing-them-wrong.html