如何将字符串转换为绑定<日期>在 DatePicker SwiftUI 中? [英] How to convert String to Binding&lt;Date&gt; in DatePicker SwiftUI?

查看:25
本文介绍了如何将字符串转换为绑定<日期>在 DatePicker SwiftUI 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想将 DatePicker 从 String 设置为出现时的某个日期.

I want to set DatePicker from a String to a certain date when it appears.

@State var staff: Staff
DatePicker(selection: $staff.birthDate, in: ...Date(),displayedComponents: .date) {
            Text("Birth Date:")
        }

我期望的是类似的东西,但它不起作用,因为选择需要 Binding 并且 $staff.birthDate 是一个字符串.我如何转换它?

What I expect is something like that, but it didn't work because selection requires Binding<Date> and $staff.birthDate is a String. How do I convert it?

我尝试创建一个 func 来格式化它:

I tried to create a func to format it like so:

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 ?? "")
}

但是还是不行.关于如何解决这个问题的任何想法?还是我的做法有误?

But it still doesn't work. Any idea on how to solve this? Or did I approach this wrong?

先谢谢你.

推荐答案

您需要使用另一个日期参数作为出生日期.所以你的 Staff 类是

You need to use another date parameter for birth date. So your Staff class is

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 扩展中创建一个日期格式器.

Now create one date formattter in the DateFormatter extension.

extension DateFormatter {
    static var formate: DateFormatter {
        let dateFormatter = DateFormatter()
        dateFormatter.dateFormat = "dd-MM-yyyy"
        return dateFormatter
    }
}

现在,在视图结构中使用 .onChange 方法.它会在每次更改日期时执行,您需要将此日期转换为字符串并将其存储在您的birthDate 字符串变量中.

Now, in view struct use the .onChange method. It will execute each time when the date is changed and you need to convert this date to a string and store it in your birthDate string 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)
        }
    }
}


如果您的项目目标来自 iOS 13,那么您可以使用自定义绑定.


If your project target is from iOS 13 then you can use custom binding.

struct ContentView: View {
    
    @StateObject var staff: Staff = Staff()
  
    var body: some View {
        DatePicker(selection: Binding(get: {staff.date},
                                      set: {
            staff.date = $0;
            staff.birthDate = DateFormatter.formate.string(from: $0)
        }), in: ...Date(),displayedComponents: .date) {
            Text("Birth Date:")
        }
    }
}

这篇关于如何将字符串转换为绑定<日期>在 DatePicker SwiftUI 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆