0
   class var sharedInstance:OAuthClass {

 dispatch_once(&Static.token, {
            Static.instance = OAuthClass ();
        });

        return Static.instance!;
    }

 How can i create Singelton class in Swift. i am using this code.

is it right?what would be the solution for this.

Matthias Bauch
  • 89,811
  • 20
  • 225
  • 247
parvind
  • 877
  • 10
  • 22

1 Answers1

1

Here you go,

The laziest and easiest way to do it is storing the singleton as a global variable:

let _SingletonSharedInstance = Singleton()

class Singleton  {
    class var sharedInstance : Singleton {
        return _SingletonSharedInstance
    }
}

You can read the details, advantages and disadvantages of this approach here in detail.

Also, please read Korey Hinton Blog.

Shamsudheen TK
  • 30,739
  • 9
  • 69
  • 102