加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Swift 4.1 – 子类UIImage

发布时间:2020-12-14 04:40:44 所属栏目:百科 来源:网络整理
导读:我在升级到 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
我在升级到 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")

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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读