正在获取数据但无法正确显示

时间:2019-01-12 11:57:34

标签: ios arrays swift

我正在获取一些数据并显示在屏幕上。

 func getAllCatogory(){
        ViewUtils.addActivityView(view: self.view)
        TransportManager.sharedInstance.AllCatogory { (dt, err) in
            ViewUtils.removeActivityView(view: self.view)
            if let _ = err{

            }else{
                do {
                    let decoder = JSONDecoder()
                    decoder.keyDecodingStrategy = .convertFromSnakeCase
                    guard let str = dt as? String else { return }
                    let res  = try decoder.decode([AllCatagories].self, from:str.data(using: .utf8)!)
                    self.allCategory = res
                    self.collectionView.reloadData()
                    print(res.count)  // getting count 2
                    print(self.allCategory.count as Any) getting count 2
                }
                catch {
                    print(error)
                }

            }
        }
    }

但是在我的收藏夹视图中,当我要追加或打印不来时。

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {

        let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "HomeCollectionCell", for: indexPath) as! HomeCollectionCell


        let ct = self.allCategory[indexPath.item]
           print(ct.menuName as Any)  // getting null
            //cell.productName.text = ct.menuName


        return cell
    }

不确定可能是什么问题。对此有任何帮助吗?

谢谢!

更新:

class AllCatagories: Codable{


    let image : String?
    let isActive : Int?
    let items : [Item]?
    let menuCode : String?
    let menuName : String?

    enum CodingKeys: String, CodingKey {
        case image
        case isActive = "is_active"
        case items
        case menuCode = "menu_code"
        case menuName = "menu_name"
    }
}

struct Item : Codable {

    let menuCode : String?
    let name : String?

    enum CodingKeys: String, CodingKey {
        case menuCode = "menu_code"
        case name
    }
}

这是我正在使用的模型类。主线程重新加载集合视图意味着确切的位置?

更新:

 [
  {
    "menu_code" : "NDS",
    "items" : [
      {
        "unit" : "Nos",
        "name" : "Chapathi\/Pulkas",
        "quantity" : 2
      },
      {
        "unit" : "Cup",
        "name" : "Palya\/Curry",
        "quantity" : 1
      }
    ],
    "is_active" : 1,
    "image" : "nds.jpg",
    "menu_name" : "Normal Diet South"
  },
  {
    "menu_code" : "NCCD",
    "items" : [
      {
        "menu_code" : "NDS",
        "name" : "Monday"
      },
      {
        "menu_code" : "NDN",
        "name" : "Tuesday"
      }
    ],
    "is_active" : 1,
    "image" : "NCCD.jpg",
    "menu_name" : "Normal Combo Corporate Diet"
  }
]

我的json响应在这里

1 个答案:

答案 0 :(得分:0)

如果指定convertFromSnakeCase,则必须删除编码键,这就是convertFromSnakeCase的目的

并尽可能声明非可选的成员/属性,相反。在这种情况下,您会收到解码错误。

struct AllCatagories: Codable {

    let image : String
    let isActive : Int
    let items : [Item]
    let menuCode : String
    let menuName : String
}

struct Item : Codable {
    let unit : String?
    let name : String
    let quantity : Int?
    let menuCode : String?
}

并在主线程上重新加载收集视图

DispatchQueue.main.async {
    self.collectionView.reloadData()
}