【发布时间】:2017-08-13 11:39:56
【问题描述】:
我有一个带有单个变量的协议
protocol Localizable {
var localizationKey: String { get set }
}
我为此实现了一个默认getter:
extension Localizable {
var localizationKey: String {
get {
assert(true, "❌ Do not use this getter!
The localizationKey is a convenience variable
for setting a localized string.")
return ""
}
}
}
现在我让几个类符合这个协议。在这些类中,我想覆盖 localizationKey 的 setter,但对其 getter 使用默认实现,例如:
extension UILabel: Localizable {
var localizationKey: String {
get {
// ❓ Use default implementation from protocol extension here
}
set {
text = LocalizedString(forKey: newValue)
}
}
}
(如何)我可以这样做吗?
【问题讨论】:
标签: swift overriding protocols extension-methods default-implementation