swift compiler error

heres the error! Cannot assign value of type 'DayData' to type '[DayData]'

here's my code in ViewController:

private func fetchData() {

        APICaller.shared.getCovidData(for: scope) { [weak self] result in

            switch result {

            case .success(let dayData):

                self?.dayData = dayData

            case .failure(let error):

                print(error)

            }

        }

    }

You don't show enough so that we can tell exactly:

  • what does getCovidData(for: scope) return ?
  • how is result defined (its struct)?
  • how is dayData defined in the class (self) ?

But likely,

  • self?.dayData is an array of DayData
  • and dayData is DayData

Hence the error.

You should have something like

self?.dayData.append(dayData)

Note: naming var with very meaningful term is very important to avoid coding errors. Hence, naming dayData something that is array in the same way that a single element is very confusing.

You'd better change for instance as:

var dayDatas : [DayData] 

or

var dayDataSeries : [DayData] 

or

var dayDataArray : [DayData] 

Siomething to remind you it is an array. With this, you would immediately see that this is wrong:

self?.dayDataSeries = dayData
swift compiler error
 
 
Q