运行代码

时间:2018-04-09 09:46:57

标签: swift charts cocoapods

当我尝试在我的应用中实施pod 图表时。它显示出以下错误。

enter image description here

import UIKit

import Charts

class chartViewController: UIViewController {

@IBOutlet weak var barChartView:BarChartView!
let 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]

override func viewDidLoad() {
    super.viewDidLoad()
   setupChart(x: months, y: unitsSold)
}

override func didReceiveMemoryWarning() {
    super.didReceiveMemoryWarning()
    // Dispose of any resources that can be recreated.
}

func setupChart(x:[String],y:[Double]){
   var dataEntries: [BarChartDataEntry] = []

    for i in 0..<x.count{
          let dataEntry = BarChartDataEntry(x: Double(i), y: y[i])
        dataEntries.append(dataEntry)
    }

    let chartDataSet = BarChartDataSet(values: dataEntries, label: "Units Sold")
    print(chartDataSet)
    let chartData = BarChartData(dataSet: chartDataSet)
    print(chartData)
  barChartView.data = chartData   // here its showing the error

}

它将最后一行代码中的错误显示为

  

线程1:运行时EXC_BAD_ACCESS(代码= 2,地址= 0x10cb21d98)   代码

1 个答案:

答案 0 :(得分:5)

  1. 选择包含带有图表的View Controller的故事板。
  2. 选择将显示图表的视图。
  3. 在“自定义类”下的“身份检查器”中,选择“类”下拉列表中的“条形图视图”。
  4. 那可以解决你的问题。

    像这样:

    enter image description here