如何在 DatePicker SwiftUI 中将 String 转换为 Binding<Date>?
How to convert String to Binding<Date> in DatePicker SwiftUI?
我想将字符串中的 DatePicker 设置为出现时的特定日期。
@State var staff: Staff
DatePicker(selection: $staff.birthDate, in: ...Date(),displayedComponents: .date) {
Text("Birth Date:")
}
我期望的是类似的东西,但它没有用,因为选择需要 Binding<Date>
而 $staff.birthDate
是一个字符串。如何转换?
我试着创建一个函数来格式化它:
func formatStringToDate(date: String?) -> Binding<Date> {
@Binding var bindingDate: Date
let dateForm = DateFormatter()
dateForm.dateFormat = "dd-MM-YYYY"
// _bindingDate = date
return dateForm.date(from: date ?? "")
}
但是还是不行。关于如何解决这个问题的任何想法?还是我做错了?
提前谢谢你。
您需要为出生日期使用另一个日期参数。
所以你的员工 class 是
class Staff: ObservableObject {
@Published var birthDate: String = "02-05-2021"
@Published var date: Date = Date()
init() {
self.date = DateFormatter.formate.date(from: birthDate) ?? Date()
}
}
现在在 DateFormatter
扩展中创建一个日期格式化程序。
extension DateFormatter {
static var formate: DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
return dateFormatter
}
}
现在,在视图结构中使用 .onChange
方法。它会在每次更改日期时执行,您需要将此日期转换为字符串并将其存储在您的 birthDate 字符串 var.
struct ContentView: View {
@StateObject var staff: Staff = Staff()
var body: some View {
DatePicker(selection: $staff.date, in: ...Date(),displayedComponents: .date) {
Text("Birth Date:")
}
.onChange(of: staff.date) { (date) in
staff.birthDate = DateFormatter.formate.string(from: date)
}
}
}
如果您的项目目标来自 iOS13,那么您可以使用自定义绑定。
struct ContentView: View {
@StateObject var staff: Staff = Staff()
var body: some View {
DatePicker(selection: Binding(get: {staff.date},
set: {
staff.date = [=13=];
staff.birthDate = DateFormatter.formate.string(from: [=13=])
}), in: ...Date(),displayedComponents: .date) {
Text("Birth Date:")
}
}
}
我想将字符串中的 DatePicker 设置为出现时的特定日期。
@State var staff: Staff
DatePicker(selection: $staff.birthDate, in: ...Date(),displayedComponents: .date) {
Text("Birth Date:")
}
我期望的是类似的东西,但它没有用,因为选择需要 Binding<Date>
而 $staff.birthDate
是一个字符串。如何转换?
我试着创建一个函数来格式化它:
func formatStringToDate(date: String?) -> Binding<Date> {
@Binding var bindingDate: Date
let dateForm = DateFormatter()
dateForm.dateFormat = "dd-MM-YYYY"
// _bindingDate = date
return dateForm.date(from: date ?? "")
}
但是还是不行。关于如何解决这个问题的任何想法?还是我做错了?
提前谢谢你。
您需要为出生日期使用另一个日期参数。 所以你的员工 class 是
class Staff: ObservableObject {
@Published var birthDate: String = "02-05-2021"
@Published var date: Date = Date()
init() {
self.date = DateFormatter.formate.date(from: birthDate) ?? Date()
}
}
现在在 DateFormatter
扩展中创建一个日期格式化程序。
extension DateFormatter {
static var formate: DateFormatter {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "dd-MM-yyyy"
return dateFormatter
}
}
现在,在视图结构中使用 .onChange
方法。它会在每次更改日期时执行,您需要将此日期转换为字符串并将其存储在您的 birthDate 字符串 var.
struct ContentView: View {
@StateObject var staff: Staff = Staff()
var body: some View {
DatePicker(selection: $staff.date, in: ...Date(),displayedComponents: .date) {
Text("Birth Date:")
}
.onChange(of: staff.date) { (date) in
staff.birthDate = DateFormatter.formate.string(from: date)
}
}
}
如果您的项目目标来自 iOS13,那么您可以使用自定义绑定。
struct ContentView: View {
@StateObject var staff: Staff = Staff()
var body: some View {
DatePicker(selection: Binding(get: {staff.date},
set: {
staff.date = [=13=];
staff.birthDate = DateFormatter.formate.string(from: [=13=])
}), in: ...Date(),displayedComponents: .date) {
Text("Birth Date:")
}
}
}