gpt4 book ai didi

Swift 3 图表条形图无

转载 作者:行者123 更新时间:2023-11-28 15:21:45 24 4
gpt4 key购买 nike

我收到以下错误:

unexpectedly found nil while unwrapping an Optional value

我的代码如下,在我的viewDidLoad中调用了该函数:

 func setChart() {

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]


var dataEntries: [BarChartDataEntry] = []


for i in 0..<months.count {

let dataEntry = BarChartDataEntry(x: Double(months[i])!, y: Double(unitsSold[i]))

dataEntries.append(dataEntry)
}

let chartDataSet = BarChartDataSet(values: dataEntries, label: "Visitor count")

let chartData = BarChartData(dataSet: chartDataSet)

barChartView.data = chartData

}

代码似乎在 let dataEntry 行中断。

最佳答案

您的月份数组包含月份的名称,您正试图将其转换为 Double,例如 x: Double("Jan")! ,这总是会崩溃,

所以先替换

Double(months[i])!Double(i)

然后获取坐标轴标签

经过与OP的讨论,我们找到了如下解决方案,可以给x轴添加标签

yourchartView.xAxis.valueFormatter = IndexAxisValueFormatter(values:months) 
yourchartview.xAxis.granularity = 1

关于Swift 3 图表条形图无,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45873165/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com