我正在尝试做这样的事情:
public extension UIImage {
public convenience init(whatever: Int) {
UIGraphicsBeginImageContextWithOptions(...)
//...
let image = UIGraphicsGetImageFromCurrentContext()
UIGraphicsEndImageContext()
return image // <- impossible
}
}
但这是不可能的,因为“nil”是初始化程序的唯一有效返回......我该怎么做?
例如,Objtive-C 方法 [UIImage imageNamed:] 是一个类方法(它可以在 Objective-C 中返回它想要的任何内容),它被映射到 swift 初始化程序 UIImage(named:)。