通过Alamofire Object Mapper Swift从映射数组中获取值

时间:2017-10-30 06:45:26

标签: ios json swift xcode objectmapper

下面是我用于使用Object Mapper映射我的JSON的代码,我成功地过滤了数组。

 Alamofire.request("https://project-isdental-cammy92.c9users.io/api/v2.0/company/1", method: .get, parameters: nil, encoding: URLEncoding.default, headers: params).responseObject { (response: DataResponse<ProductGroup>) in
        let ProductGroupRes = response.result.value

        if let Product = ProductGroupRes?.products
        {    
            self.collectionArray = Product.filter({$0.group_type == 1}) as NSArray
            print(self.collectionArray)

            self.tableArray = Product.filter({$0.group_type == 2}) as NSArray


        }

    }

这是我的过滤数组,即collectionArray

"IndiaSupply.Product(group_title: Optional(\"Recommended\"), 
group_type: Optional(1), 
products: Optional([IndiaSupply.ProductArray(
         product_id: Optional(1), 
         product_name: Optional(\"Product 1\"),
         product_price: Optional(\"Rs 1,999/-\"), 
         product_category: Optional(\"Surgical\"),
         product_image: Optional(\"\"),
         product_description: Optional(\"Description 1\")),
        IndiaSupply.ProductArray(product_id: Optional(2),
         product_name: Optional(\"Product 2\"),
         product_price: Optional(\"Rs 1,999/-\"), 
         product_category: Optional(\"Surgical\"), 
         product_image: Optional(\"\"), 
         product_description: Optional(\"Description 1\")),
         IndiaSupply.ProductArray(product_id: Optional(3), 
         product_name: Optional(\"Product 3\"), 
         product_price: Optional(\"Rs 1,999/-\"),
         product_category: Optional(\"Surgical\"), 
         product_image: Optional(\"\"),
         product_description: Optional(\"Description 1\")),
      IndiaSupply.ProductArray(product_id: Optional(4),
       product_name: Optional(\"Product 4\"), 
       product_price: Optional(\"Rs 1,999/-\"), 
       product_category: Optional(\"Surgical\"), 
       product_image: Optional(\"\"), 
       product_description: Optional(\"Description 1\")),

          )

如何从此结果中获取产品数组,然后从中获取价值。我无法使用此代码获取它:

let Pro = self.collectionArray.value(forKey : "products") as NSArray

1 个答案:

答案 0 :(得分:1)

你成功地映射了它,现在你必须使用这些模型对象,如

func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell {
   //....
   let aProduct = self.collectionArray[indexPath.item] as! Product
   cell.labelName.text = aProduct.product_name
   //and so on

}