struct User: Codable,Identifiable,Hashable{
var @DocumentID var id: String?
var name: String
var birthDay: Timestamp?
}
struct Profile: View {
@Binding var user: User
@State var birthDay: Date
init(){
self._birthDay = State(initialValue: _user.wrappedValue.birthDay as! Timestamp
}
}
I don't know how to write the down casting from Firestore's Timestamp to Date.
I got the error Cannot convert value of type 'Timestamp' to expected argument type 'Date'
in self._birthDay = State(initialValue: _user.wrappedValue.birthDay as! Timestamp
.
If _user.wrappedValue.birthDay
is nil, I want to set the current date.
I checked this But this is possible for nil. I want to set the current date if it is nil. And User's birthday is optional. So I want to set jest Date type.
I think I can write this
let timestamp = _user.wrappedValue.birthDay as! Timestamp
let date = timestamp.dateValue()
But I don't know if the timestamp is nil how to set current date.
I tried writing the down casting. But I can't get success. Especially I don't know how to write that in 1 statement.