4
Alamofire.request(APPURL.GetAccounts, method: .post, 
                      parameters: transactionData,
                      encoding: JSONEncoding.default,
                      headers: nil).responseJSON { responseData in }

My question is what is the reason that restricts me to only pass Dictionary of type [String:Any]?

Complete Code:

class func getAccounts( transactionData: [String:Any]?, withCompletionHandler: @escaping (_ response:AnyObject?)->(Void) ) {

    Alamofire.request(APPURL.GetAccounts, method: .post, 
                      parameters: transactionData,
                      encoding: JSONEncoding.default,
                      headers: nil).responseJSON { responseData in

        if((responseData.result.value) != nil) {
            let swiftyJsonVar = JSON(responseData.result.value!)
            if let resData = swiftyJsonVar["Accounts"].arrayObject {
                let resultData = resData as! [[String:AnyObject]]
                withCompletionHandler(resultData as AnyObject)
            }else{
                withCompletionHandler(nil)
            }
        }else{
            withCompletionHandler(nil)
        }
    }
}
1
  • You are describing a "limitation" of alamofire and not swift 4 itself. Plus, this is not a limitation. When you talk about parameters in alamofire is always intended a structure key value because it is impossible to handle in a generic way custom requests. If you have a custom post body, you can still do it by adding setting the postBody var of the request that is returned by alamofire. Please update your title. This is not a swift 4 related question. you want help using alamofire. Commented Feb 12, 2018 at 11:12

1 Answer 1

0

In JSON format the key has always to be string and the value can be of any type. This is how JSON works in Swift.

Sign up to request clarification or add additional context in comments.

1 Comment

Was not aware of this one thanks! I was used to pass String there.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.