2

How can I fix the reason why the warning appears?

struct ContentView: View {

    @State private var selectedDate: Date = Date()

    var body: some View {
        Form {
            DatePicker("Date", selection: $selectedDate, displayedComponents: .date)
        }
    }
}
Isaak
  • 1,107
  • 2
  • 11
  • 29
  • This log seems to appear only when the `DatePicker` is inside a `Form` view, I've tried to put the `DatePicker` in a `VStack` only, and no more warning. – cedricbahirwe May 13 '21 at 08:10
  • Note that your code is working fine, Sometimes, it's just `SwiftUI` which is printing some stuff on your console. – cedricbahirwe May 13 '21 at 08:12

1 Answers1

4

A workaround solution would be to use a List View instead of a Form. It all depends on what you want to put in your Form. For demonstration purpose, using a List, your code would look like this:

struct ContentView: View {

    @State private var selectedDate: Date = Date()

    var body: some View {
        List {
            DatePicker("Date", selection: $selectedDate, displayedComponents: .date)
        }
        .listStyle(InsetGroupedListStyle())
    }
}

The above gives the same visual effect (UI) as using a List, and no warning is shown.

Because everything is working correctly with your Form, I don't really see the need to change your Form to a List just to avoid the logs.

cedricbahirwe
  • 1,274
  • 4
  • 15
  • In my case a `List` actually makes more semantic sense than a `Form`. Thank you. – Isaak May 13 '21 at 08:33
  • Thanks @cedricbahirwe, using list instead of form didn't solve the warning (I still get it), but some toggles with conditional content are finally working correctly. Using form the conditional content didn't appear sometimes. – G. Marc Feb 27 '22 at 06:20