title: Swift 3 , CollectionView, Alamofire 4 : finding nil while unwrapping a value
body: <p>Here is my <strong>JSON</strong> response </p>
  
  <pre><code>{
  "category_list" : [
  {
    "category_imag" : "http:\/\/xxx\/wp-content\/uploads\/2016\/10\/img-thing.jpg",
    "category_name" : "category 1",
    "category_description" : "dsadsadas",
    "category_id" : 6
  },
  {
    "category_imag" : "http:\/\/xxx\/wp-content\/uploads\/2016\/09\/7075hp_2_2.jpg",
    "category_name" : "category 2",
    "category_description" : "dasdadasdas",
    "category_id" : 12
    }
   ]
  }
  items are***********************************************
  {
    "category_imag" : "http:\/\/xxx\/wp-   content\/uploads\/2016\/10\/img-thing.jpg",
   "category_description" : "dsadsadas",
   "category_name" : "category 1",
   "category_id" : 6
  }
  {
   "category_imag" : "http:\/\/xxx\/wp-  content\/uploads\/2016\/09\/7075hp_2_2.jpg",
   "category_description" : "dasdadasdas",
  "category_name" : "category 2",
  "category_id" : 12
    }
   items number***********************************************
  2
  fatal error: unexpectedly found nil while unwrapping an Optional value
  (lldb) 
  </code></pre>
  
  <p>here is the function I am using to fetch <strong>JSON</strong> using <strong>Alamofire</strong> &amp; <strong>SwiftyJSON</strong> </p>
  
  <pre><code>func getJSON(){
  
      let getEndPoint: String = "http://xxx/api/category_list/"
      Alamofire.request(getEndPoint)
          .responseJSON { response in
  
              guard response.result.error == nil else {
                  // got an error in getting the data, need to handle it
                  print("error calling GET")
                  print(response.result.error!)
                  return
              }
  
              if let value = response.result.value {
  
                  let json = JSON(value)
                  print(json)
                  // print(json["main_content"].array)
                  let categoryList = json["category_list"].arrayValue
                  for items in categoryList {
  
                      //self.productList += [HomeFeedJSON(items:items )]
                      self.getCategoryList.append(CategoryList(items: items))
                      print(items)
  
                  }
  
                  print("items are***********************************************")
                  print(self.getCategoryList.count)
  
                  DispatchQueue.main.async {
                      self.afData = 1
                      self.collectionView?.reloadData()
                  }
  
              }
      }
  
  }
  </code></pre>
  
  <p>and here is the model <strong>class</strong> I am using for assigning the values fetched from <strong>JSON</strong> </p>
  
  <pre><code>import Foundation
  import SwiftyJSON
  
  class CategoryList {
  
  var category_name: String?
  var category_imag: URL?
  var category_description: String?
  
  
  init(items: JSON){
      self.category_name = items["category_name"].stringValue
      self.category_imag = items["category_imag"].URL
      self.category_description =    items["category_description"].stringValue
  
    }
  
  }
  </code></pre>
  
  <p>I don't want to bother people but I have tried all possible way I could, its showing <strong>nil</strong> on my <strong>collectionViewCell</strong> . in <strong>numberOfItemsInSection</strong> I am getting intended result but at <strong>cellForItemAt indexPath</strong> it shows <strong>nil</strong> value and crashes. Here is my <strong>cellForItemAt indexPath</strong></p>
  
  <pre><code>  override func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -&gt; UICollectionViewCell {
  
      let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "CatagoryShopCell", for: indexPath) as! CatagoryShopCell
  
      if afData == 1 {
  
        let myCategory = self.getCategoryList[indexPath.row]
  
        cell.catagoryName.text = myCategory.category_name
        cell.catagoryDesc.text = myCategory.category_description
  
      let categoryImg = myCategory.category_imag
      SDWebImageManager.shared().downloadImage(with: categoryImg, options: [], progress: nil) { downloadedImage, error, cacheType, isDownloaded, url in
              cell.catagoryPicture.image = downloadedImage
          }
      }
  
      return cell
  }
  </code></pre>
  
  <p>I have used the same function/model class in a <strong>tableView</strong> in it works just fine, but I can't sort it out how its not finding value in my model class here at <strong>collectionView</strong> . Please any kind person help. Thank you in advance.</p>