需要一点帮助,我对iOS开发有点新手,当前遇到了一个小问题。
我正在尝试将我从JSON文件中获取的值传递到另一个方法,因此我可以生成barGraph,但是我有问题将它作为字符串数组传递。
这是视图控制器类,我试图找到一种方法来完成它,请同时看到我创建的2个类,我可以确认值都在那里。
查看控制器类 -
import UIKit
import Charts
class ViewController: UIViewController, ChartViewDelegate {
@IBOutlet weak var barChartView: BarChartView!
let model:JsonModel = JsonModel()
var data:[Data] = [Data]()
var censusYear:[String]! = []
var currentData:Data = Data()
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
barChartView.delegate = self
// Kick off the data gathering
self.data = self.model.getData()
// Kick off the graph generation
setChart(<#T##[String]#>, values: <#T##[Double]#>)
}
func setChart(dataPoints: [String], values: [Double]) {
barChartView.noDataTextDescription = " 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: "Population")
let chartData = BarChartData(xVals: censusYear, dataSet: chartDataSet)
barChartView.data = chartData
barChartView.descriptionText = "This is a test"
chartDataSet.colors = ChartColorTemplates.colorful()
barChartView.animate(xAxisDuration: 2.0, yAxisDuration: 2.0, easingOption: .EaseInBounce)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
数据类 -
class Data: NSObject {
var area:String = ""
var censusYear:String = ""
var sex:String = ""
var population:Double = 0
}
JSON数据模型类 -
class JsonModel: NSObject {
func getData() -> [Data] {
// Get array of data objects
var data:[Data] = [Data]()
// Get JSON array of dictionaries
let jsonObjects:[NSDictionary] = self.getLocalFile()
// Loop through each dictionary and assign values to the data object parse JSON File
var index:Int
for index = 0; index < jsonObjects.count; index++ {
// Current JSON Dict
let jsonDictionary:NSDictionary = jsonObjects[index]
// Create a Data object
let d:Data = Data()
// Assign value to each key value pair to the data object
d.area = jsonDictionary["area"] as! String
d.censusYear = jsonDictionary["census year"] as! String
d.sex = jsonDictionary["sex"] as! String
d.population = jsonDictionary["population"] as! Double
// Add the data into data array
data.append(d)
}
// Return list of data object
return data
}
func getLocalFile() -> [NSDictionary] {
// Get an NSURL object pointing to the JSON filed in the app bundle
let appBundlePath:String? = NSBundle.mainBundle().pathForResource("sample3", ofType: "json")
// TODO: Use optional binding or guard to check if path exisits
if let actualBundlePath = appBundlePath {
// Path exists
let urlPath:NSURL = NSURL(fileURLWithPath: actualBundlePath)
let jsonData:NSData? = NSData(contentsOfURL: urlPath)
if let actualJsonData = jsonData {
// NSData exists, use the NSJSONSerialization classes to parse the data and create the dictionaries
do {
let arrayOfDictionaries:[NSDictionary] = try NSJSONSerialization.JSONObjectWithData(actualJsonData, options: NSJSONReadingOptions.MutableContainers) as! [NSDictionary]
return arrayOfDictionaries
}
catch {
// There was an error parsing the json file
}
}
else {
// NSData doesnt exist
}
}
else {
// Path to json file in the app bundle doesnt exist
}
// Return an empty array
return [NSDictionary]()
}