数组不存储值
Array not storing values
我正在从解析后端取回值并使用它们创建一个数组,但其中没有存储任何内容。有人可以告诉我我做错了什么吗?该行:let dataEntry = ChartDataEntry(value: values[i], xIndex: i) 返回 "index is out of range"
var fattyArray: [Double] = []
override func viewDidLoad() {
super.viewDidLoad()
let innerQuery = PFUser.query()
innerQuery!.whereKey("objectId", equalTo: "VTieywDsZj")
let query = PFQuery(className: "BodyFat")
query.whereKey("UserID", matchesQuery: innerQuery!)
query.findObjectsInBackgroundWithBlock {
(percentages: [PFObject]?, error: NSError?) -> Void in
if error == nil {
print("Successful, \(percentages!.count) retrieved")
if let percentage = percentages as [PFObject]! {
for percentage in percentages! {
print(percentage["BodyFatPercentage"])
self.fattyArray.append(percentage["BodyFatPercentage"].doubleValue)
print(self.fattyArray)
}
}
} else {
print("\(error?.userInfo)")
}
}
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = fattyArray
setChart(months, values: unitsSold)
}
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)
}
这里有两个问题:
一个问题是 findObjectsInBackgroundWithBlock
异步运行,即 fattyArray
直到稍后才附加值。您应该将调用 setChart
的代码移动到 findObjectsInBackgroundWithBlock
闭包中。
query.findObjectsInBackgroundWithBlock { percentages, error in
if error == nil {
// build your array like you did in your question
// but when done, call `setChart` here
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = fattyArray
setChart(months, values: unitsSold)
} else {
print("\(error?.userInfo)")
}
}
// but not here
// let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
// let unitsSold = fattyArray
//
// setChart(months, values: unitsSold)
您的 setChart
例程给您一个 "index is out of range" 错误,因为您正在遍历 dataPoints
,但在 values
数组中查找值.我在这里看不到任何东西可以保证 months
中的条目数与 fattyArray
.
中的条目数相同
很明显,如果你修复了第一点,它就更有可能起作用(因为在数据完全检索之前你实际上不会生成图表),但我仍然看不到这里任何保证 findObjectsInBackgroundWithBlock
将 return 至少六个条目的内容。
我正在从解析后端取回值并使用它们创建一个数组,但其中没有存储任何内容。有人可以告诉我我做错了什么吗?该行:let dataEntry = ChartDataEntry(value: values[i], xIndex: i) 返回 "index is out of range"
var fattyArray: [Double] = []
override func viewDidLoad() {
super.viewDidLoad()
let innerQuery = PFUser.query()
innerQuery!.whereKey("objectId", equalTo: "VTieywDsZj")
let query = PFQuery(className: "BodyFat")
query.whereKey("UserID", matchesQuery: innerQuery!)
query.findObjectsInBackgroundWithBlock {
(percentages: [PFObject]?, error: NSError?) -> Void in
if error == nil {
print("Successful, \(percentages!.count) retrieved")
if let percentage = percentages as [PFObject]! {
for percentage in percentages! {
print(percentage["BodyFatPercentage"])
self.fattyArray.append(percentage["BodyFatPercentage"].doubleValue)
print(self.fattyArray)
}
}
} else {
print("\(error?.userInfo)")
}
}
let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"]
let unitsSold = fattyArray
setChart(months, values: unitsSold)
}
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)
}
这里有两个问题:
一个问题是
findObjectsInBackgroundWithBlock
异步运行,即fattyArray
直到稍后才附加值。您应该将调用setChart
的代码移动到findObjectsInBackgroundWithBlock
闭包中。query.findObjectsInBackgroundWithBlock { percentages, error in if error == nil { // build your array like you did in your question // but when done, call `setChart` here let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] let unitsSold = fattyArray setChart(months, values: unitsSold) } else { print("\(error?.userInfo)") } } // but not here // let months = ["Jan", "Feb", "Mar", "Apr", "May", "Jun"] // let unitsSold = fattyArray // // setChart(months, values: unitsSold)
您的
中的条目数相同setChart
例程给您一个 "index is out of range" 错误,因为您正在遍历dataPoints
,但在values
数组中查找值.我在这里看不到任何东西可以保证months
中的条目数与fattyArray
.很明显,如果你修复了第一点,它就更有可能起作用(因为在数据完全检索之前你实际上不会生成图表),但我仍然看不到这里任何保证
findObjectsInBackgroundWithBlock
将 return 至少六个条目的内容。