Properties

This is a good time to talk briefly about getters and setters in Swift. In Swift, you can have a property of a class that is a getter or a setter or both. To see how this works, you’ll create a class called Human, and then you’ll see what it means to get and set properties of that class:

class Human {    var eyeColor = "#00FF00"    var heightInInches = 68    var hairLengthInCM = 2.54    var name = "Skip"}

This class has four properties. You may or may not want all of these properties to be able to be rewritten. For example, once the eye color is set, you might only want that to be able to be read and not written to. There are also other details you can get from these properties being set. For example, ...

Get Learning Swift™ Programming now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.