2

I am trying to create a class method which optionally returns an (already instantiated) instance of the class.

I'm thinking of something like writing the memory address of the instantiated class to a user default key and then trying to return the object at that address but I'm not sure if that's a correct approach or how to do that.

class MyClass {

    let myProperty: String

    required init(myProperty: String) {
        self.myProperty = myProperty
    }

    class func currentClass() -> MyClass? {
        return nil
    }
}

let aNewClass = MyClass(myProperty: "Hi")

// Should return the aNewClass instance:
MyClass.currentClass()
luk2302
  • 55,258
  • 23
  • 97
  • 137
Fred Faust
  • 6,696
  • 4
  • 32
  • 55
  • 2
    Sounds like a singleton class to me which is highly disregarded due to the way garbage collection works in swift (i.e., reference counting). A singleton class results in a reference circle that will never be freed.. Here are some hints: http://stackoverflow.com/questions/24024549/dispatch-once-singleton-model-in-swift/24147830#24147830 http://stackoverflow.com/questions/33524951/how-to-destory-a-singleton-in-swift – Florian Blum Jan 07 '16 at 20:52
  • @FlorianWeßling I agree in general about singletons, but it's worth saying that singletons are usually not freed anyway. – Caleb Jan 07 '16 at 20:57
  • @Caleb yeah, sure. In case this is wanted, my second link might be helpful: http://stackoverflow.com/questions/33524951/how-to-destory-a-singleton-in-swift – Florian Blum Jan 07 '16 at 20:59
  • @FlorianWeßling while the class this code exemplifies is almost a singleton, it's not quite and I believe I avoided the reference cycle by clearing the static property in luk2302 's answer below by declaring it as weak and then setting it to nil on deinit. – Fred Faust Jan 07 '16 at 21:02
  • @thefredelement ah great idea! but I don't see the `weak` keyword. Did I miss something? – Florian Blum Jan 07 '16 at 21:04
  • @FlorianWeßling it is not there :) but I have done it in Xcode. Ty for the great comments. – Fred Faust Jan 07 '16 at 21:07
  • @thefredelement all right! you are welcome :) – Florian Blum Jan 07 '16 at 21:08

1 Answers1

3

You need to create a static property which you assign the already created member to:

class MyClass {
    let myProperty: String
    private static var instance : MyClass?

    required init(myProperty: String) {

        self.myProperty = myProperty

        if MyClass.instance == nil {
            MyClass.instance = self
        }
    }

    class func currentClass() -> MyClass? {
        return MyClass.instance
    }
}

let aNewClass = MyClass(myProperty: "Hi")

This is somewhat similar to the singleton pattern, you can always retrieve the same object back from the class. You could alternatively remove the if around the assignment causing the Class to always return the latest instance if that better suits your need.

luk2302
  • 55,258
  • 23
  • 97
  • 137
  • Ty, this is great and works well, do you know of any implications of declaring the static property weak? – Fred Faust Jan 07 '16 at 20:54
  • @thefredelement making it weak will just cause it to be potentially `nil` during the runtime of the app whenever no other objects holds any reference to it any more. If you can deal with that or even *want* that, go ahead. – luk2302 Jan 07 '16 at 20:57
  • Actually on de init I can clear the static property which seems to avoid a reference cycle. – Fred Faust Jan 07 '16 at 20:58
  • 1
    @thefredelement reference cycle is not the correct term here, since there is not real circle, you simply hold the reference forever, since the static variable lives forever. What exactly is your concern? – luk2302 Jan 07 '16 at 20:59
  • that the MyClass instance would never be released as long as the static property referred to it. – Fred Faust Jan 07 '16 at 21:02
  • 2
    @thefredelement Yes, that is true, but is that a problem? There will be **one** instance in memory at all time. It will get cleared when the app is terminated. That is not an issue and perfectly acceptable. – luk2302 Jan 07 '16 at 21:05