“Ambiguous use of 'propertyName'” error given overridden property with didSet observer

前端 未结 3 1986
面向向阳花
面向向阳花 2021-01-01 10:50

I\'ve got a simple scenario in which I have a parent class, Person, which defines a property called \"name\" and includes a \"didSet\" observer...

class Pers         


        
3条回答
  •  独厮守ぢ
    2021-01-01 11:04

    I was able to work around this in my case by casting to the base class:

    (employee as Person).name = "Sally"
    

    This still appears to do the proper dispatch to the subclass. For instance:

    class Person {
        var name: String {
                return "person"
        }
    }
    
    class Employee: Person {
        override var name: String {
            return "employee"
        }
    }
    
    let bob = Person()
    let alice = Employee()
    
    println((alice as Person).name) // prints "employee"
    

提交回复
热议问题