6 Replies
      Latest reply on May 18, 2018 2:54 AM by Claude31
      0xfeedface Level 1 Level 1 (0 points)

        I have try to override variable in subclass many times, but never try override getter/setter before today, this is my Swift code in Playground :

         

        class Person {
            var name: String {
                didSet {
                    print("Superclass did Set")
                }
                willSet {
                    print("Superclass will Set")
                }
            }
            init(name: String) {
                self.name = name
            }
        }
        
        class Student: Person {
            override var name: String {
                didSet {
                    print("Subclass did Set")
                }
                willSet {
                    print("Subclass will Set")
                }
            }
        }
        
        let apple = Student(name: "John")
        apple.name = "Apple"

         

        The results like below:

         

        Subclass will Set

        Superclass will Set

        Superclass did Set

        Subclass did Set

         

        That's mean we can't override variable setter/getter?