iOS-Charts error: Thread1: EXC_BAD_ACCESS (code=2, address=0x2a0c220)
iOS-Charts error: Thread1: EXC_BAD_ACCESS (code=2, address=0x2a0c220)
所以,我正在学习 AppCoda 上的 iOS-Charts 教程,我完全遵循了它,然后当我遇到问题时,我也复制了代码以完全匹配它们。
问题是,当我尝试创建饼图时,然后 运行 它。我收到错误:
Thread1: EXC_BAD_ACCESS (code=2, address=0x2a0c220)
然后应用程序崩溃了。当它崩溃时,它会突出显示这一行:
pieChartView.data = pieChartData
屏幕截图如下:
这是来自 viewcontroller.swift 的完整代码:
import UIKit
import Charts
class ViewController: UIViewController {
@IBOutlet weak var pieChartView: PieChartView!
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartView.data = pieChartData
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setChart(months, values: unitsSold)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
看来您可能错过了这一部分:
Next drag a View onto the Bar Chart View Controller and pin its edges as shown. This View is a child of the main View in the controller.
您需要在您的视图控制器中创建一个 UIView,然后 link 将其添加到 @IBOutlet weak var pieChartView: PieChartView!在你的代码中。
然后就可以了。
它看起来像一个不错的图书馆。
希望对您有所帮助。请告诉我们。
编辑:
转到故事板上的视图控制器并拖动一个 UIView。然后 select UIView 并在身份检查器中将其 class 设置为 PieChartView。打开 ViewController swift 文件,右键单击顶部 @IBOutlet 行上的小圆圈,然后按住右键将其拖到刚刚创建的 UIView 上,然后离开。现在您将 outlet 连接到代码,它会将 pieChartView var 识别为一个对象,而不是当前的 nil。
我用相同的代码遇到了完全相同的错误。
正如 smozgur 所建议的,我检查了 Iboutlet 连接,但似乎连接正常
问题是我设置了错误的模块,所以也要检查 "module target"
我希望这对某人有帮助:)
所以,我正在学习 AppCoda 上的 iOS-Charts 教程,我完全遵循了它,然后当我遇到问题时,我也复制了代码以完全匹配它们。
问题是,当我尝试创建饼图时,然后 运行 它。我收到错误:
Thread1: EXC_BAD_ACCESS (code=2, address=0x2a0c220)
然后应用程序崩溃了。当它崩溃时,它会突出显示这一行:
pieChartView.data = pieChartData
屏幕截图如下:
这是来自 viewcontroller.swift 的完整代码:
import UIKit
import Charts
class ViewController: UIViewController {
@IBOutlet weak var pieChartView: PieChartView!
func setChart(dataPoints: [String], values: [Double]) {
var dataEntries: [ChartDataEntry] = []
for i in 0..<dataPoints.count {
let dataEntry = ChartDataEntry(value: values[i], xIndex: i)
dataEntries.append(dataEntry)
}
let pieChartDataSet = PieChartDataSet(yVals: dataEntries, label: "Units Sold")
let pieChartData = PieChartData(xVals: dataPoints, dataSet: pieChartDataSet)
pieChartView.data = pieChartData
var colors: [UIColor] = []
for i in 0..<dataPoints.count {
let red = Double(arc4random_uniform(256))
let green = Double(arc4random_uniform(256))
let blue = Double(arc4random_uniform(256))
let color = UIColor(red: CGFloat(red/255), green: CGFloat(green/255), blue: CGFloat(blue/255), alpha: 1)
colors.append(color)
}
pieChartDataSet.colors = colors
}
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = [20.0, 4.0, 6.0, 3.0, 12.0, 16.0]
setChart(months, values: unitsSold)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
看来您可能错过了这一部分:
Next drag a View onto the Bar Chart View Controller and pin its edges as shown. This View is a child of the main View in the controller.
您需要在您的视图控制器中创建一个 UIView,然后 link 将其添加到 @IBOutlet weak var pieChartView: PieChartView!在你的代码中。
然后就可以了。 它看起来像一个不错的图书馆。
希望对您有所帮助。请告诉我们。
编辑:
转到故事板上的视图控制器并拖动一个 UIView。然后 select UIView 并在身份检查器中将其 class 设置为 PieChartView。打开 ViewController swift 文件,右键单击顶部 @IBOutlet 行上的小圆圈,然后按住右键将其拖到刚刚创建的 UIView 上,然后离开。现在您将 outlet 连接到代码,它会将 pieChartView var 识别为一个对象,而不是当前的 nil。
我用相同的代码遇到了完全相同的错误。
正如 smozgur 所建议的,我检查了 Iboutlet 连接,但似乎连接正常
问题是我设置了错误的模块,所以也要检查 "module target" 我希望这对某人有帮助:)