iOS图表,Swift 2.1.1 Xcode 7.2似乎不起作用,

时间:2016-01-08 08:46:05

标签: ios xcode swift ios-charts

我在两周内遇到了麻烦,所以请让我提问。

场合

XCode 7.2 Apple Swift 2.1.1版(swiftlang-700.1.101.15 clang-700.1.81) https://github.com/yosuke1985/ios-charts

iOS图表 TTPS://github.com/danielgindi/ios-charts

此代码来自

TTP://www.appcoda.com/ios-charts-api-tutorial/

我想要什么

  

我想使用此框架,但错误是EXEC_BAC_ACCESS,

我只将Window对象放在ViewBtroller by StoryBoard上, 并连接为BarChartView类。

想要显示条形图, 当我更改属性时," noDataText"和" noDataTextDescription" 它不起作用,无法弄清楚错误来自。

    barChartView.noDataText = "there is no data"
    barChartView.noDataTextDescription = "give me the reason"

只有这些代码不起作用, 我开始认为问题来自Swift或XCode的版本? 请给我建议解决这个问题。

错误

EXEC_BAC_ACCESS ERROR, Screen Shot

import UIKit
import Charts

class ViewController: UIViewController {
var months: [String]!


@IBOutlet weak var barChartView: BarChartView!
override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    barChartView.noDataText = "there is no data"
    barChartView.noDataTextDescription = "give me the reason"
    self.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)
}


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


    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
}


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


}

1 个答案:

答案 0 :(得分:5)

解决。您需要设置条形图类视图,让故事板知道要初始化哪个类。自定义类现在为空,这意味着它将初始化为UIView并用作BarChartView。 见http://i.imgur.com/c4tLNjL.png