Good day, friends.
Once again stupid question about Obj-C from newbie 🙂
I’m trying to implement singleton design pattern in Obj-C:
@interface SampleSingleton : NSObject {
@private
static SampleSingleton* instance;
}
+(SampleSingleton*) getInstance;
Compiler returns error: “expected specifier-qualifier-list before ‘static'”.
Please find below the Objective-C code snippet I am using, for proper thread-safe singleton implementation
header file :
implementation file :
Just to help you starting in objective-c and not get lost in your project structure, you can consider having the project structure matching your file system so as your project becomes bigger you won’t get lost.
Also please consider using a proper class naming convention, and stick to it.
I a providing you mine as sample:
Any class that match singleton pattern is named using Manager suffix (E.g. MyCustomManager ).
Any static class is named using Helper suffix (E.g. MyCustomHelper).
Any class dedicated to control particular process is named using Controller suffix ( E.g. MyParticularTaskConstroller ).
Any UI control that inherit from another control needs provide control suffix ( E.g. MyCustomDetailCell inheriting from UITableViewCell )
Hope this helps.