当前位置 : 主页 > 手机开发 > 其它 >

Swift 4.1 – 子类UIImage

来源:互联网 收集:自由互联 发布时间:2021-06-11
我在升级到 Swift 4.1后使用自定义init创建子类UIImage时,不支持覆盖扩展的非@ objc声明错误 class Foo: UIImage { init(bar: String) { } required init?(coder aDecoder: NSCoder) { fatalError("init(coder:) has not been i
我在升级到 Swift 4.1后使用自定义init创建子类UIImage时,不支持覆盖扩展的非@ objc声明错误

class Foo: UIImage {

    init(bar: String) { }

    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // Overriding non-@objc declarations from extensions is not supported
    required convenience init(imageLiteralResourceName name: String) {
        fatalError("init(imageLiteralResourceName:) has not been implemented")
    }
}

谢谢你的帮助

extension UIImage {

    /// Creates an instance initialized with the given resource name.
    ///
    /// Do not call this initializer directly. Instead, initialize a variable or
    /// constant using an image literal.
    required public convenience init(imageLiteralResourceName name: String)
}

此init方法在UIiamge类的扩展上进行了分解.

The error pretty much says that if a function is declared in the extension than it can’t be overridden in this way

class Foo: UIImage {

}

extension Foo {
    convenience init(bar :String) {
        self.init()
    }
}

var temp = Foo(bar: "Hello")

你可以尝试以这种方式实现欲望的结果.

网友评论