I'm thinking that you might be able to just remove the static
keyword from your computed property,
then remove this whole line
@State private var datesRead = formatDates
and just rename your computed property datesRead
instead of formatDates
So you would end up with this...
@State private var startDate = Date()
@State private var endDate = Date()
var datesRead: String {
let formatter = DateFormatter()
formatter.dateStyle = .medium
let date1 = formatter.string(from: startDate)
let date2 = formatter.string(from: endDate)
return "\(date1)-\(date2)"
}
I don't think you need datesRead to be an @State property, because you don't need your view to be updated every time it changes. You are already using @State on your other two dates, so your view would be updated any time one of those is changed, and the computed property would automatically have its new value at that point as well.
Also, I don't think that you need datesRead to be a property in your data model at that point, because it can just be computed from other data that you already have in there. But, I think it is in the next project that you will learn how to add manually written extensions to your data model Entities, which will allow you to create computed properties like this in your Entity's definition, rather than creating it in a separate View's definition like we are doing here.