I have a class that is constructed with NSData
, and I would like to construct a convenience init that reads the NSData from a file. Naturally, that might fail, so the convenience init must be an init?
, but I can't see how to construct it. What I've tried is
convenience init?(path: String, encoding: NSStringEncoding) {
if let data = NSData(contentsOfFile: path) {
self.init(data: data, encoding: encoding)
} else {
return nil
}
}
but I get the error "All stored properties of a class must be initialized before returning nil from an initializer". It seems pointless to set up all of the properties to dummy values and then just return nil. What am I missing?