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.

Models in SwiftUI

Models in SwiftUI

- [Narrator] While it's easy to make user interfaces in SwiftUI, it's not as easy to link them to your models. This week we'll look at one way to do that. If you look at the downloaded exercise file I have a single file app to make them easier to follow. I have a simple toggle button I made in an earlier tip. Above it I have a simple model of a boolean value that, once made true, goes false three seconds later. This model's not a struct, but a class. That way, the properties can change. In the button I have a state variable. State variables allow a property to be mutable within a struct. They also trigger a redraw when their value is changed. In this case, every time we press the button, the view redraws itself with a new value. State variables are internal to the view only. Core to SwiftUI variables is an idea of a single source of truth, the only place where this value is stored. But what if you wanted the content view…

Contents