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

前端 未结 3 1954
面向向阳花
面向向阳花 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"
    
    0 讨论(0)
  • 2021-01-01 11:23

    I got this error because I had two properties declared with the same name.

    On Objective-C the compiler used to give the error on the line the property was declared. Something like "duplicate property".

    Now on Swift you get "Ambiguous use of..." on the line that you use to property... and you have to look everywhere to find the duplicate property.

    0 讨论(0)
  • 2021-01-01 11:29

    As @sgaw points out, this has been confirmed as a known bug by the Apple engineers (for Xcode 6 Beta - Version 6.0 (6A215l))

    https://devforums.apple.com/thread/229668?tstart=0

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