I found out from this post we can create fixed size array as such
var array = [String?](repeating: nil, count: 10)
How can I do the same, when the parameter count
is passed in from different view.
I have a primary view
where user selects a start date
and end date
.
I pass the dates to secondary view
.
Using information from this post I figured out how to get all days between two dates.
I want to create a state variable as such @State private var names: [String?](repeating: nil, count: DayCount)
Where DayCount
is total days.
struct SecondaryView: View {
// loop start and end date are passed from primary view
var loopStartDate = Date()
var loopEndDate = Date()
@State private var dateArray = Date.dates(from: loopStartDate, to: loopEndDate)
// throws cannot use instance member 'loopEndDate' within property initializer;
//property initializers run before 'self' is available
@State private var names = [String?](repeating: nil, count: dateArray.count)
// this throws error `Cannot use instance member 'dateArray' within property
//initializer; property initializers run before 'self' is available`