Mel*_*Mel 5 nsdictionary swift swift-extensions
我正在尝试在Swift中扩展类NSDictionary以包含在init()上设置的NSDate.当我添加自定义init()时,我得到编译器错误:
'required'初始化程序'init(dictionaryLiteral :)'必须由'NSDictionary'的子类提供
但是,当我使用自动完成添加初始化程序时,我收到以下错误:
来自扩展的声明无法覆盖
有没有办法覆盖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")
}
}
Run Code Online (Sandbox Code Playgroud)
Swift 有一个向类添加方法的官方扩展机制,但是当子类重写扩展方法时,编译器会引发错误。不过,错误文本看起来很有希望(添加了强调):
\n\n来自扩展的声明还不能被覆盖\nIt\xe2\x80\x99s 悬挂的 \xe2\x80\x9cyet\xe2\x80\x9d 这鼓励我相信 Apple\xe2\x80\x99s 工程师知道这样的设计模式受保护的扩展模式,并将更新 Swift 以支持它们。
\n\n检查\n https://github.com/ksm/SwiftInFlux/blob/master/README.md#overriding-declarations-from-extensions
\n| 归档时间: |
|
| 查看次数: |
1123 次 |
| 最近记录: |