call a setter on an instance of a class in swift

To call a setter on an instance of a class in Swift, you simply assign a new value to the property using dot notation. Here's an example class with a simple property and its setter:

class MyClass {
    var myProperty: String = ""

    func setMyProperty(newPropertyValue: String) {
        myProperty = newPropertyValue
    }
}
146 chars
8 lines

To call the setter setMyProperty on an instance of MyClass, you would do the following:

let myInstance = MyClass()
myInstance.setMyProperty(newPropertyValue: "Hello, world!")
87 chars
3 lines

However, a better option is to directly set the property to the new value using the dot notation:

let myInstance = MyClass()
myInstance.myProperty = "Hello, world!"
67 chars
3 lines

This will call the setter method automatically and update the property value.

gistlibby LogSnag