这是我的代码
extension UIImage {
convenience init(color: UIColor, size: CGSize = CGSizeMake(1, 1)) {
let rect = CGRectMake(0, 0, size.width, size.height)
UIGraphicsBeginImageContext(rect.size)
let context = UIGraphicsGetCurrentContext()
CGContextSetFillColorWithColor(context, color.CGColor)
CGContextFillRect(context, rect)
let image = UIGraphicsGetImageFromCurrentImageContext()
UIGraphicsEndImageContext()
init(CGImage: image.CGImage!)
}
}
在 init(CGImage: image.CGImage!)
上,我得到了错误
Initializers may only be declared within a type
最佳答案
专用初始化器从便利初始化器调用
使用 self.init(...)
:
self.init(CGImage: image.CGImage!)
没有self.
(或super.
,如果你调用父类(super class)初始化器)
编译器将 init(...)
误认为是 init 的 declaration
方法。
关于ios - 错误 : Initializers may only be declared within a type,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34328602/