singleton vs Singleton

PHOTO EMBED

Tue Apr 09 2024 07:24:08 GMT+0000 (Coordinated Universal Time)

Saved by @Saurabh_Lodhi #swift #inapppurchase

//Here is signleton - that means you can create an object of a class outside it

class singleton{
  
  static let shared = singleton()
  init(){} //(Optional to do it or not)
  
  let temp = 5

}

//Usage

viewDidLoad(){
  let num = singleton.shared.temp
  
  //but also you can create object
  let obj = singleton()
  let num = obj.temp
}


//Here is Signleton - that means you cannot create an object of a class outside it

class Singleton{
  
  static let shared = singleton()
  private init(){} //(Optional to do it or not)
  
  let temp = 5

}

//Usage

viewDidLoad(){
  let num = singleton.shared.temp
  
  //but you cannot create object
  //let obj = singleton()
  //let num = obj.temp
}
content_copyCOPY