Swift property observer in protocol extension?

前端 未结 1 1221
一个人的身影
一个人的身影 2020-12-31 00:40

Consider the following:

protocol ViewControllable: class {
  typealias VM: ViewModellable
  var vm: VM! { get }
  func bind()
}

extension ViewControllable          


        
相关标签:
1条回答
  • 2020-12-31 01:09

    No, this is explicitly disallowed. See Extension: Computed Properties:

    Extensions can add new computed properties, but they cannot add stored properties, or add property observers to existing properties.

    Keep in mind that if this were legal, it would add some non-trivial confusion about order of execution. Imagine there were several extensions that added didSet, and the actual implementation also had a didSet. What order should they run in? This doesn't mean it's impossible to implement, but it could be somewhat surprising if we had it.

    0 讨论(0)
提交回复
热议问题