ChartsOrg / ChartsOrg/Charts

Precondition failed: Down-casted Array element failed to match the target type

Open
#4,469 0 comments 0 reactions 0 assignees View on GitHub
Dominant language
Swift
Stars
28k
Forks
6k
PR merge metrics
No merged PRs in 30d

Description

I have created a sample project and I integrated Charts. I am using Swift5 and I am getting a crash with this error:

**Precondition failed: Down-casted Array element failed to match the target type
Expected BarChartDataEntry but found ChartDataEntry: file Swift/ArrayBuffer.swift**

Here is my code:

`let sales = DataGenerator.data()
var salesEntries = [ChartDataEntry]()
var salesMonths = [String]()
var i = 0

for sale in sales {
let saleEntry = ChartDataEntry(x: Double(i), y: sale.value)
salesEntries.append(saleEntry)

salesMonths.append(sale.month)

i + 1
}
let chartDataSet = BarChartDataSet(entries: salesEntries, label: "km")
let chartData = BarChartData(dataSets: [chartDataSet])
barChart.data = chartData`

And, I have created a structure for the Data:

`struct Sale {
var month: String
var value: Double
}

class DataGenerator {

static var randomizedSale: Double {
return Double(arc4random_uniform(10000) + 1) / 10
}

static func data() -> [Sale] {
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
var sales = [Sale]()

for month in months {
let sale = Sale(month: month, value: randomizedSale)
sales.append(sale)
}
return sales
}
}`

Can someone please help me with this?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.