Search code examples
swiftclassconstructorinitializer

Initializer in a Swift Class


I have come to Swift from other languages. There is a pattern I sometimes use in defining a class in which the initializer calls another function which would be called later to re-initialize.

class Test {
    var thing: String
    init() {
        reinit()    //  'self' used in method call 'load' before all stored properties are initialized
    }               //  Return from initializer without initializing all stored properties
    func renit() {
        self.thing = "Hello"
    }
}

In this case, it doesn’t work. I really don’t want to write the initializing code twice, and I can’t use test.init(), apparently to re-intialize.

What is the correct way of doing this?

Update

A few comments suggest that this is a bad design, and it may well be. In reality, of course, the application will not be so trivial.

The point is that I can do it this way in other languages, so I’m wondering what is the correct approach in Swift.


Solution

  • If your object can be "re-initialized" then just initialize it to some base-state by default:

    class Test {
        var thing: String = "" // Add default value
        init() {
            reinit()
        }          
    
        func reinit() {
            self.thing = "Hello"
        }
    }
    

    As a rule, I think this is probably a bad design. Instead of "reinitializing," replace this class with a struct, and just throw it away and replace it when you want to reset things. As a rule, structs are very cheap to create. But if you need this, then default values is ok.