SwiftUI · DatePicker & Toggle | raywenderlich.com

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

Why do we need to make the @State variable private? I can understand that @State is to make the view to synchronise with the variable. But how about the “private” part?

Hey. So that parent views cannot mutate it. State should be private to the view that they affect. We got over this in the course as well. Laurie.

Hi, my “Select Date” text is not laying out properly (on Canvas or actual device). I’ve attached a screenshot of the the problem (running on iPhone SE 2020 in dark mode) as well as the code. Any idea as to why this problem is occurring?



Hey, I could be wrong about this but I believe it’s a fairly new addition to SwiftUI and you need to pass it a date component style modifier. See here for stuff to try out: https://developer.apple.com/documentation/swiftui/datepickerstyle

I cannot understand here what is ‘DatePicker.Components’ in the date picker initializer. Could you please help?

@shrutisharma yes the date components concerns the formatting of the displayed date. Currently we can show the date or the hourAndMinute. One shows the full date and the other would show the time so-to-speak. Currently the documentation isn’t super-helpful but hopefully that is clear. Take care! Laurie.