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 } }
If someProperty can be nil, then I think you want to define the property as:
var someProperty: NSString?
This also eliminates the need for a custom initializer (at least, for this property), since the property doesn't require a value at initialization time.
In complement to the answers, a good idea is to call super.init() before other statements. I think it's a stronger requirement in Swift because allocations are implicit.