O'Reilly logo

Object–Oriented Programming with Swift 2 by Gastón C. Hillar

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Combining setters, getters, and a related property

Sometimes, we want to have more control over the values that are set to properties and retrieved from them, and we can take advantage of getters and setters to do so. In fact, we can combine a getter and setter, which generate a computed property and a related property that stores the computed value, and access protection mechanisms to prevent the user from making changes to the related property and force him to always use the computed property.

The superhero's sneakers might change over time. However, we always have to make sure that the sneakers' name is an uppercase string. We can define a sneakers property with a getter method that always converts the string value to an uppercase string and ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required