I am used to Java
and currently on the process of learning Objective-C
.
Basically I would create Singleton
classes in Java
like this:
public class SingletonClass{
private static instance; //Step 1
public static SingletonClass getInstance(){ //Step 2
if(instance == null)
instance = new SingletonClass();
return instance;
}
}
Very straightforward right?
But I find a hard time creating straight forward solution for that in Objective-C
I did like this:
@implementation SingletonClass(){
//I want to do step 1 here which is to make a private static instance;
//it is said that private variables are declared here
static SingletonClass *instance; //but it is said that static keyword is different here
}
//then I would do something like step 2
+ (id)getInstance{
if(instance == nil)
instance = self;
return instance;
}
@end
The problem is that there is an error :Type name does not allow storage class to be specified
How do you guys make straight forward Singleton classes in Objective-C?