我有一个带有单个变量的协议
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
's setter 但对其 getter 使用默认实现,例如:
extension UILabel: Localizable {
var localizationKey: String {
get {
// ❓ Use default implementation from protocol extension here
}
set {
text = LocalizedString(forKey: newValue)
}
}
}