From the course: iOS Development Tips

Unlock this course with a free trial

Join today to access over 22,600 courses taught by industry experts.

Setter and Getters

Setter and Getters

- [Instructor] Other programming languages require to use setters and getters. Swift does not but you can use them. Let's take a look at some cases for setters and getters. Now, I'm going to use the area for pizza for an example. Pizzas are sized by diameter but we're going to need to radius to get an area. If you download the exercise files, you'll see a simple playground of three variables. The simplest getter is often referred to as computed property. Computed properties are read only properties where you add a closure after them which return a value for the property. For example, let me use the radius here. And I'll put a closure and just add size divided by 2.0. Again, notice this is Swift 5.1 so I don't need the return here but if you're using something earlier than that you'll probably need to put return size slash 2.0. Now, to compute the area, I can do the same thing. Or I could use a formal getter.…

Contents