-1

I'm relatively new to swift and searched around but could not find any satisfactory answer to my problem. I would like to have a Singleton class instance which can be initialized with some variables. E.g.

public class Singleton {
   var car: String
   var bus: String

   init(car: String, bus: String) {
    self.car = car
    self.car = bus
   }

   func drive() {
       print("I will drive")
   }
}


public class SingletonConsumer {
  // create an instance of Singleton Once
  var driver: Singleton = Singleton(car: "honda", bus: "volvo")
  driver.drive()
}



public class driverClassWorld : SingletonConsumer {
   driver.drive()
}

how can i achieve it? I tried protocol but issue i am hitting is how to instantiate singleton class with parameters.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
iamMobile
  • 959
  • 2
  • 17
  • 35
  • This is a regular class. Why do you call it a singleton? In a realistic case, where would `"honda"` and `"volvo"` come from? – Sergey Kalinichenko Sep 18 '18 at 23:44
  • Try to make your variable immutable static - static let driver: Singleton = Singleton(car: "honda", bus: "volvo") – Evgeny Karkan Sep 18 '18 at 23:57
  • `car`/`bus` are poor examples for a singleton. I'd bet if you look deeper you won't be able to find a valid example for tor this kind of singletons, which is a good indicator that you don't need this in the first place. – Cristik Sep 19 '18 at 05:30
  • Does this answer your question? [Singleton and init with parameter](https://stackoverflow.com/questions/28429544/singleton-and-init-with-parameter) – Confused Vorlon Jul 06 '20 at 15:59

1 Answers1

0

I don't get this problem?

First remove singleton from your brain for a moment. Because I think you have the wrong idea on what a singleton is.

Now lets rephrase your question to: "How to instantiate a class with parameter"

Its like this:

import Foundation

class Test {

    let someText : String!

    init(something:String){
        someText = something
    }

    func test(){
        print("TEST \(someText)")
    }
}

let a = Test(something: "Me")
a.test()

Output:

TEST Optional("Me")

You just need to define a init with the parameters you want.

Now to properly instantiate a singleton (basically it just the class above but single instance). There are a lot of ways, the old Objective C approach is still valid.

But for swift this is the most common pattern. You need to define a Static Property.

Example:

import Foundation

class Test {

    static let shared = Test(something: "REAL SINGLETON")

    let someText : String!

    init(something:String){
        someText = something
    }

    func test(){
        print("TEST \(someText)")
    }
}

Test.shared.test()

Output:

TEST Optional("REAL SINGLETON")

Now reread the definition of a singleton:

a singleton class is a class that can have only one object (an instance of the class) at a time

For other patterns in declaring a singleton:

https://cocoacasts.com/what-is-a-singleton-and-how-to-create-one-in-swift

Now, you might wonder: When does this singleton instance instantiated?

Answer: It is when it is first used/called.