State & Binding | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/18272812-swiftui-fundamentals/lessons/5

Hi Catie, Could you please explain the private word a bit? What is the reason for wrapping those @State variables private? So they never get used outside that view? is it the only reason? Many thanks

Its a way to control access to data. You can read more in the Apple Swift book - chapter - “Access Control”. They give some simple examples to follow along.

Marking a variable private essentially confines its use to inside the struct(or class). So in side the struct where it is defined you use it as you normally would any variable. but from outside, the struct, you would not be able to access this variable. So in a sense it makes your code safer and explicitly controls “access” to the variables/data.

I’m looking for a simple way to remember when to use the @Binding or the @State property wrapper. Can you help?