如何使用私有集扩展协议?

Hon*_*ney 2 protocols private swift swift-extensions swift-protocols

我正在编写一个具有readOnly标签的协议.我想扩展它并给它一个默认的实现,其中符合类型是a 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
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

但是,当我添加private之前,set我得到以下错误.

期望'get','set','willSet'或'didSet'关键字来启动访问者定义

我查看了这个错误的其他问题,但没有发现它们与我的相关.

Str*_*ers 5

你只是在错误的地方私人:

private(set) var keyboardLabel : UILabel {
    get {
        let label = UILabel()
        label.textColor = UIColor.white
        label.translatesAutoresizingMaskIntoConstraints = false

        return label
    }
    set {
        keyboardLabel = newValue
    }
}
Run Code Online (Sandbox Code Playgroud)