我正在编写一个带有只读标签的协议(protocol)。我想扩展它并给它一个默认实现,其中符合类型是 UITextView
。
代码:
protocol CountingView {
var keyboardLabel : UILabel {get}
}
extension CountingView where Self : UITextView {
var keyboardLabel : UILabel {
get {
let label = UILabel()
label.textColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}
private (set) {
keyboardLabel = newValue
}
}
}
但是,当我在 set
之前添加 private
时,出现以下错误。
Expected 'get', 'set', 'willSet', or 'didSet' keyword to start an accessor definition
我查找了其他有这个错误的问题,但没有找到与我相关的问题。
最佳答案
你只是把 private 放在了错误的地方:
private(set) var keyboardLabel : UILabel {
get {
let label = UILabel()
label.textColor = UIColor.white
label.translatesAutoresizingMaskIntoConstraints = false
return label
}
set {
keyboardLabel = newValue
}
}
关于swift - 如何使用私有(private)集扩展协议(protocol)?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44115637/