I'm trying pass date use Binding. This is where i'm getting this error next to Text
Cannot convert value of type 'Binding' to expected argument type 'Date'
if I remove style: .date i'm getting another error
Initializer 'init(_:)' requires that 'Binding' conform to 'StringProtocol'
struct ProfileView<ViewModel: ViewModelType>: View {
@ObservedObject var viewModel: ViewModel
@State var selectedDate: Date = Date()
init(with viewModel: ViewModel) {
self.viewModel = viewModel
}
var body: some View {
VStack(alignment: .leading) {
Text( Binding<Date>(
get: { viewModel.selectedDate },
set: { _ in viewModel.setDate(selectedDate) }), style: .date )
}
}
}
final class ViewModel: ViewModelType {
var selectedDate: Date = Date()
func setDate(_ selectedDate: Date) {
self.selectedDate = selectedDate
}
}
DatePicker("", selection: $selectedDate, displayedComponents: .date)
protocol ViewModelType: ObservableObject {
var selectedDate: Date { get }
func setDate(_ selectedDate: Date)
}
I wold like set the date use func setDate()
CodePudding user response:
The error you get is because your Text(...)
in ProfileView
requires a Date
, but you are giving it a Binding<Date>
.
I suggest you try the following code to set the selectedDate
in your
ViewModel
. The @Published var selectedDate: Date
will get updated
automatically in your views, whenever the viewModel
is changed. There is no need for a separate func setDate(...)
.
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
VStack {
ProfileView(viewModel: viewModel)
DatePicker("", selection: $viewModel.selectedDate, displayedComponents: .date)
}
}
}
struct ProfileView: View {
@ObservedObject var viewModel: ViewModel
var body: some View {
VStack(alignment: .leading) {
Text(viewModel.selectedDate, style: .date )
}
}
}
final class ViewModel: ObservableObject {
@Published var selectedDate: Date = Date()
}