2

Please help me with Swift, I need singleton with can inheritance. I can do like this

class A {
    var defaultPort: Int
    required init() {
        self.defaultPort = 404
    }
    class var defaultClient: A {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: A? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = self.init()
        }
        return Static.instance!
    }
}

but in swift 2.0 we can do like this

static let defaultClient = A() //self.init() 

but it creates an instance of the class A any way. How i can use like this self.init()

static let defaultClient = self.init()

in order to be able to inherit

UPD best way for now

class A {
    class func defaultClient() -> Self {
        struct Static {
            static var onceToken: dispatch_once_t = 0
            static var instance: A? = nil
        }
        dispatch_once(&Static.onceToken) {
            Static.instance = self.init()
        }
        return instance(Static.instance, asType: self)
    }
}

here we need helper as

func instance<T>(instance: Any, asType type: T.Type) -> T {
    let reurnValue = instance as! T
    return reurnValue
}

because another way cast A to Self not exist, for now.

p.s. crazy swift way! why i can not do instance as! Self

EvGeniy Ilyin
  • 1,817
  • 1
  • 21
  • 38
  • do the answers in these [very](http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift/24147830#24147830) [related questions](http://stackoverflow.com/questions/27144915/swift-singleton-inheritance) help you out? – Michael Dautermann Oct 30 '15 at 22:02
  • Unfortunately no, but my post(code#1) answer that question – EvGeniy Ilyin Oct 30 '15 at 22:16
  • You've answered [here](http://stackoverflow.com/questions/27144915/swift-singleton-inheritance) with the same thing. So, is this a question or an answer? Is the answer only for Swift 1? Please explain a bit more. – Eric Aya Oct 30 '15 at 22:23
  • its different question. here question in possible - Singleton Inheritance. my question in Singleton in **one line on Swift 2.0** with Inheritance. – EvGeniy Ilyin Oct 30 '15 at 22:27
  • Thanks. So, the answer you posted on the other page works, but you want it as one line in swift 2? – Eric Aya Oct 30 '15 at 22:29
  • **self.init()** good work in **class var/class func**, but it is don't work in **static let** (compilation error) – EvGeniy Ilyin Oct 30 '15 at 22:31
  • yes i want it as one line in swift 2 – EvGeniy Ilyin Oct 30 '15 at 22:32
  • it was written in the subject header :) – EvGeniy Ilyin Oct 30 '15 at 23:22

1 Answers1

4

Your question isn't very clear. You're looking for something like the class constant solution posted in this answer, but which automatically uses "my own class" instead of explicitly creating an instance of a specific class... right?

That is, you want to turn this:

class Singleton  {
    static let sharedInstance = Singleton()
}

into this:

class Singleton  {
    static let sharedInstance = SomeMagicThing()
}
class SingletonSubclass {}

where SomeMagicThing automatically creates a Singleton instance when you call Singleton.sharedInstance, and a SingletonSubclass instance when you call SingletonSubclass.sharedInstance. Correct?

Sorry, that can't be done (as of Swift 2.1).

Part of your issue is that static and class mean two different things. The static modifier means that the declaration it modifies is associated only with a specific type declaration. So, the Singleton type owns a pointer to a specific object -- its subclasses don't inherit that pointer. (And if they did, would it point to the same object or a subclass-specific one?)

If you could create a class var or class let, that'd (in theory) give you the kind of dispatch/inheritance you want. But trying that gives you an error (emphasis mine):

class stored properties not yet supported in classes; did you mean static?

So it sounds like this sort of thing might show up someday.

Of course, the other side of the problem is finding a way to dynamically refer to the "current" type responsible for executing some statement. In the context of an instance method, you have self.dynamicType for such things... but there's no equivalent for classes. (Self is a type constraint, not an actual type.) This is a side effect of the type system in Swift being much more strict and static than that of Objective-C (for example, metatypes aren't just a special flavor of otherwise normal objects). File a bug if you'd like to see a change to that effect?

Community
  • 1
  • 1
rickster
  • 124,678
  • 26
  • 272
  • 326