Your First iOS and SwiftUI App · SwiftUI Bindings | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4919757-your-first-ios-and-swiftui-app/lessons/17

Can you please explain why we reference the slider state/binding variable using self.$sliderValue, but earlier in this tutorial series we referenced the button alert state/binding variable using $alertIsVisible? Why do we use self to reference one but not the other?

There are two times you need to use self. The first is during initialization when your parameters match your instance variables. The other time is when you want to access an instance variable inside of a closure.

When Ray referenced the alertIsVisible in the button, he needed to the use self since he was setting the value inside a closure. He didn’t need it when he passed it into the Slider, but it’s not usual to see that happen out of habit from other languages like Java or C#.