How to implement a failable initializer for a class conforming to NSCoding protocol?
I'm getting the following errors:
1. Line override init() {}
: Property 'self.videoURL' not initialized at implicitly generated super.init call
2. Line return nil
: All stored properties of a class instance must be initialized before returning nil from an initializer
I've seen Best practice to implement a failable initializer and All stored properties of a class instance must be initialized before returning nil which helped me a lot, but since my class also conforms to NSCoding protocol I don't know how to implement a failable initializer in my case.
Any suggestions on how to implement a failable initializer?
class CustomMedia : NSObject, NSCoding {
var videoTitle: String?
let videoURL: NSURL!
override init() {}
init?(title: String?, urlString: String) {
// super.init()
if let url = NSURL(string: urlString) {
self.videoURL = url
self.videoTitle = title
} else {
return nil
}
}
func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeObject(self.videoTitle, forKey: PropertyKey.videoTitle)
aCoder.encodeObject(self.videoURL, forKey: PropertyKey.videoURL)
}
required init(coder aDecoder: NSCoder) {
videoTitle = aDecoder.decodeObjectForKey(PropertyKey.videoTitle) as? String
videoURL = aDecoder.decodeObjectForKey(PropertyKey.videoURL) as! NSURL
}
}