I'm trying to create a subclass of UIImage with an added property, but when I create a convenience init, I can't call the UIImage's designated init init(named name: String) because for some reason it's not inherited.
class myUIImage: UIImage {
var imageType$: String?
convenience init(imageName$ imageName$: String) {
self.init(...?)
self.imageType$ = // ...
}
}
Any ideas?
There is "not inherited" string in named: initializer declaration:
public /*not inherited*/ init?(named name: String) // load from main bundle
I am not sure what "not inherited" exactly stands for, but looks like its real nature is "convenience initializer" and you cannot use it in subclasses. At least it behaves like this. So, I'd propose you to go the following way:
class TheImage: UIImage {
var param: String! = nil
convenience init?(param: String) {
guard let image = UIImage(named: "TheImage") where nil != image.cgImage else {
return nil
}
self.init(cgImage: image.cgImage!)
self.param = param
}
}
From other hand, usually you don't really need to subclass UIImage. In most of cases creating a wrapper class would be sufficient and much more appropriate.