Search code examples
iosswiftalamofire

Calling API through Alamofire but having some issue


I am calling api through Alamofire and I am passing request params perfectly. I have checked that but still I am not getting response. I am getting success message but don't get response data. Below is my code,

let API = "http://siestalaminates.com/admin/index.php/Webservice/getcategorybybrandid"
let subcate = ["brand_id": brandid] as [String:Any]
        Alamofire.request(API, method: .post, parameters: subcate ).responseJSON
            {
                response in
                //printing response
                print(response)
                let result = response.result
                // let obj=result
                if let dict = result.value as? Dictionary<String,AnyObject>{
                    if let subdata = dict["data"]{
                        self.subcatelist = subdata as! [AnyObject]
                        self.subcategory_collection.reloadData()

                    }
                }
                self.activityIndicator.stopAnimating()
        }

Pass Brand id dynamically::

func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) {
        ///print("==>",cateid[indexPath.row] as NSString)

            let vc = self.storyboard?.instantiateViewController(withIdentifier: "ProductsubcategoryViewController") as! ProductsubcategoryViewController

            vc.brandid = (self.brandid[indexPath.row] as NSString) as String?


            self.present(vc, animated: false, completion: nil)
    }

If I pass brand_id statically then I am getting response but when I pass it dynamically then it pass perfectly but not getting response.


Solution

  • Replace the following line:

       let subcate = ["brand_id": brandid] as [String:Any]
    

    With:

       let subcate = ["brand_id": brandid!] as [String:Any]
    

    It seems only issue you have is optional and required values. I think you're not good with optional and required chaining.