-1

Below is my code I used to map my JSON using Object Mapper, I successfully filtered arrays.

 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


        }

    }

This is my filtered array i.e. 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\")),

          )

How I can fetch array of products from this result, and then get value from it. I couldn't fetch it with this code:

let Pro = self.collectionArray.value(forKey : "products") as NSArray
Shikha Sharma
  • 451
  • 1
  • 5
  • 25
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. **I have downvoted this time** since [I have asked this before](https://stackoverflow.com/q/46924727/472495). – halfer Oct 30 '17 at 11:13

1 Answers1

1

Its great you mapped it successfully, now you have to just use these model objects like,

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

}
Baig
  • 4,737
  • 1
  • 32
  • 47