0

我在条形图中显示我的响应数据,但问题是我得到了正确的响应,如果我传递静态数据,它会在图表中显示,但如果我传递我的响应值,那么它不会显示。这是我的代码

代码::

let usersubcategory = ["user_id": 77 ,"access_token": "f7abee7bffa89898755174dbc6548bd2","account_id": "Mike50430315","year": 2018] as [String : Any]

print(usersubcategory)
Alamofire.request(CallAPI, method: .post, parameters: usersubcategory).responseJSON
    {
        response in

        print(response)
        let result = response.result

        if let dict = result.value as? Dictionary<String,AnyObject>{
            if let categorylist = dict["data"]{

                self.searchlist = categorylist as! [AnyObject]


                 for item in self.searchlist{
                 let value = item["month"]
                 guard let value_chart = value else {
                 continue
                 }
                 let optionalvalue = value_chart
                 if let noLongerOptional = optionalvalue {
                 print("\(noLongerOptional)")
                    let chartConfig = BarsChartConfig(valsAxisConfig: ChartAxisConfig(from: 0, to: 800, by: 100))
                    let frame = CGRect(x: 0, y: 270, width: self.view.frame.width, height: 450)
                    let chart = BarsChart(frame: frame,
                    chartConfig: chartConfig,
                    xTitle: "Months",
                    yTitle: "Count",
                    bars: [
                    ("Jan", noLongerOptional as! Double),
                    ("Feb", noLongerOptional as! Double),
                    ("Mar", noLongerOptional as! Double),
                    ("Apr", noLongerOptional as! Double),
                    ("May", noLongerOptional as! Double),
                    ("Jun", noLongerOptional as! Double),
                    ("July",noLongerOptional as! Double),
                    ("Aug", noLongerOptional as! Double),
                    ("Sep", noLongerOptional as! Double),
                    ("Oct", noLongerOptional as! Double),
                    ("Nov", noLongerOptional as! Double),
                    ("Dec", noLongerOptional as! Double)
                        ],
                                          color: UIColor.darkGray,
                                          barWidth: 15
                    )
                    self.view.addSubview(chart.view)
                    self.chartView = chart
                 }
              }

            }
        }

}

我正在解开该值,然后传入图表,但是当我打印 noLongerOptional 时,我正在获取数据但无法传入图表。

静态值在图中正确显示。谁能帮帮我吗?

4

1 回答 1

0

Alamofire 在它创建的网络线程而不是主线程上返回网络响应。您需要将 UIView 操作分派到主线程

print(usersubcategory)
Alamofire.request(CallAPI, method: .post, parameters: usersubcategory).responseJSON
    {
        response in

        print(response)
        let result = response.result

        DispatchQueue.main.async {
          if let dict = result.value as? Dictionary<String,AnyObject>{
            if let categorylist = dict["data"]{

                self.searchlist = categorylist as! [AnyObject]


                 for item in self.searchlist{
                 let value = item["month"]
                 guard let value_chart = value else {
                 continue
                 }
                 let optionalvalue = value_chart
                 if let noLongerOptional = optionalvalue {
                 print("\(noLongerOptional)")
                    let chartConfig = BarsChartConfig(valsAxisConfig: ChartAxisConfig(from: 0, to: 800, by: 100))
                    let frame = CGRect(x: 0, y: 270, width: self.view.frame.width, height: 450)
                    let chart = BarsChart(frame: frame,
                    chartConfig: chartConfig,
                    xTitle: "Months",
                    yTitle: "Count",
                    bars: [
                    ("Jan", noLongerOptional as! Double),
                    ("Feb", noLongerOptional as! Double),
                    ("Mar", noLongerOptional as! Double),
                    ("Apr", noLongerOptional as! Double),
                    ("May", noLongerOptional as! Double),
                    ("Jun", noLongerOptional as! Double),
                    ("July",noLongerOptional as! Double),
                    ("Aug", noLongerOptional as! Double),
                    ("Sep", noLongerOptional as! Double),
                    ("Oct", noLongerOptional as! Double),
                    ("Nov", noLongerOptional as! Double),
                    ("Dec", noLongerOptional as! Double)
                        ],
                                          color: UIColor.darkGray,
                                          barWidth: 15
                    )
                    self.view.addSubview(chart.view)
                    self.chartView = chart
                 }
              }

            }
          }
        }
}

这就是为什么 UIView 操作必须在主线程上进行:为什么 UIKit 操作必须在主线程上进行?

于 2018-08-02T12:11:02.697 回答