Xcode 6.4 iOS图表API错误

时间:2015-08-24 09:14:38

标签: ios xcode ios-charts

我遇到了未解决的标识符问题' dataPoints' 它处于for循环中,我尽我所能,但由于我对swift很新,我似乎无法找到解决方案。

    var months: [String]!
func setChart(dataPoints: [String], values: [Double]) {
    barChartView.noDataText = "You need to provide data for the chart."

}

override func viewDidLoad() {
    super.viewDidLoad()

    months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec"]
    let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0, 4.0, 18.0, 2.0, 4.0, 5.0, 4.0]

    setChart(months, values: unitsSold)
    var dataEntries: [BarChartDataEntry] = []
    for i in 0..<dataPoints.count {
        let dataEntry = BarChartDataEntry(value: values[i], xIndex: i)
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(yVals: dataEntries, label: "Units Sold")
    let chartData = BarChartData(xVals: months, dataSet: chartDataSet)
    barChartView.data = chartData

1 个答案:

答案 0 :(得分:1)

您在for循环中使用dataPoints,但viewDidLoad()范围内没有此类变量。

在你的评论中,你提到在函数dataPoints中声明setChart(),但这是该函数的本地函数,它不适用于其他函数。

从您的代码中,您似乎应该使用months作为for循环。

就此而言,在for循环中创建unitsSold对象时,您可能打算使用values而不是BarChartDataEntry