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

无法在Swift中覆盖NSDictionary的初始化程序

发布时间:2020-12-14 02:25:05 所属栏目:百科 来源:网络整理
导读:我正在尝试在 Swift中扩展类NSDictionary以包含在init()上设置的NSDate.当我添加自定义init()时,我得到编译器错误: ‘required’ initializer ‘init(dictionaryLiteral:)’ must be provided by subclass of ‘NSDictionary’ 但是,当我使用自动完成添加初
我正在尝试在 Swift中扩展类NSDictionary以包含在init()上设置的NSDate.当我添加自定义init()时,我得到编译器错误:

‘required’ initializer ‘init(dictionaryLiteral:)’ must be provided by
subclass of ‘NSDictionary’

但是,当我使用自动完成添加初始化程序时,我收到以下错误:

Declarations from extensions cannot be overridden yet

有没有办法覆盖NSDictionary的初始化程序或者Swift只是不能处理它?

这是我的班级:

class DateParam : NSDictionary {
    let date : NSDate

    init(date: NSDate) {
        super.init()
        self.date = date
    }

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

    required convenience init(dictionaryLiteral elements: (NSCopying,AnyObject)...) {
        fatalError("init(dictionaryLiteral:) has not been implemented")
    }
}
Swift有一个官方扩展机制,用于向类添加方法,但是当子类重写扩展方法时,编译器会引发错误.但错误文本看起来很有希望(重点补充):

来自扩展的声明无法覆盖
悬挂“尚未”鼓励我相信Apple的工程师了解受保护的扩展模式等设计模式,并将更新Swift以支持它们.

校验
https://github.com/ksm/SwiftInFlux/blob/master/README.md#overriding-declarations-from-extensions

(编辑:李大同)

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

    推荐文章
      热点阅读