Subclassing NSObject in Swift – Best Practice with Initializers

I’m not Swift ninja but I would write MyClass as:

class MyClass: NSObject {
    
    var someProperty: NSString // no need (!). It will be initialised from controller 
    
    init(fromString string: NSString) {
        self.someProperty = string
        super.init() // can actually be omitted in this example because will happen automatically.
    }
    
    convenience override init() {
        self.init(fromString:"John") // calls above mentioned controller with default name
    }        
}

See the initialization section of the documentation

Leave a Comment

Hata!: SQLSTATE[HY000] [1045] Access denied for user 'divattrend_liink'@'localhost' (using password: YES)