You can add a setter to a subclass in Swift by using a computed property with a setter. Here's an example:
main.swift661 chars31 lines
In this example, we have a Person
class with a name
property, and a Employee
subclass that adds a salary
property. We then use a computed property with a getter to calculate the bonus, and a setter to set the salary based on the bonus. We also override the init
method to call the superclass init method. Finally, we create an instance of the Employee
class, set the bonus
property, and print the salary
property to verify that it was set correctly.
gistlibby LogSnag