[英]swiftui datefield like birthday in contacts
我想在苹果的联系人应用程序中创建一个日期字段,例如生日字段。 我怎样才能正确地意识到它? 日期字段必须是可选的,因此可以留空。
到目前为止我的代码
let bind = Binding<String>(
get: {Formatters.dateFormatter.string(from: birthDate ?? Date())},
set: {birthDate = Formatters.dateFormatter.date(from: $0)}
)
TextField("xxx", text: bind)
我的问题:这不是可选的,我不能将该字段留空。 但是,当我将 getter 设置为Formatters.dateFormatter.string(from:birthDate!)
时,birthDate 为 nil 时会出错。 如果我省略感叹号,Xcode 会抱怨。
背景信息: birthDate
是 Date 类型的核心数据属性,所以我需要将日期存储到数据库中。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.