如何从 alamofire 中的 json 数组中获取对象?

Posted

技术标签:

【中文标题】如何从 alamofire 中的 json 数组中获取对象?【英文标题】:How to get object from json array in alamofire? 【发布时间】:2018-04-22 11:09:55 【问题描述】:

我使用 SwiftyJSON 解析 JSON , 解析json后我的collectionview不加载数据, 为什么这不起作用?

我的要求

  Alamofire.request(url).responseJSON response in
                let json = JSON(rawValue: response.result.value!)

                do 
                    //create json object from data

                    let Packages = json!["packagesData"].array
                    for package in Packages! 
                        let titles = (package["title"]) as! String
                        self.tts.append(titles)
               
                    print(self.tts)



                 catch let error 
                    print(error.localizedDescription)
                

       

我的收藏视图

 func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell 
                let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "customCell", for: indexPath) as! MyCellCollectionViewCell

                cell.tit.text = tts[indexPath.row] as? String

                return cell

    

【问题讨论】:

我认为你应该编辑你的问题。要特别。你想要什么,你得到什么样的错误? 只需在for loop下添加这个self.collectionView.reloadData() 【参考方案1】:

你必须重新加载收藏

for package in Packages! 
   let titles = (package["title"]) as! String
   self.tts.append(titles)

self.collectionView.reloadData()

【讨论】:

以上是关于如何从 alamofire 中的 json 数组中获取对象?的主要内容,如果未能解决你的问题,请参考以下文章