Abrar
Abrar

Reputation: 68

Multi parameters Dictionary (collection list) Like: [[String: Any]] to Alamofire Parameters

Hi i am trying to give to alamofire parameters called "Dict" that are in dictionary...dictionary can contain 3 or X items. I am trying to use FOR cycle to ad dictionary to another one set of items, but...it only shows the last one...that seems it override the previous one. I tried everything I know...Even try to use SwiftyJSON framework....but alamofire only take pure dictionary type.

    var Dict = [[String: Any]]()
    
    Dict.removeAll()
    
    for (index, value) in _SurveySubmitModel.enumerated() {
        print("Item \(index + 1): \(value)")
        
        let parameters: [String: Any] = [
            "ID": value.ID,
            "SurveyID": value.SurveyID,
            "QuestionID": value.QuestionID,
            "FilledBy": value.FilledBy,
            "Response": value.Response
        ]
        
        Dict.append(parameters)
        
    }
    
    print("Dict = \(Dict)")

well I need something like this

 [{
  "ID": 0,
  "SurveyID": 25,
  "QuestionID": 28,
  "FilledBy": 7335,
  "Response": "1In the two weeks before you felt sick, did you:"
 },
 {
  "ID": 0,
  "SurveyID": 25,
  "QuestionID": 28,
  "FilledBy": 7335,
  "Response": "1In the two weeks before you felt sick, did you:"
 }
 ]

Upvotes: 2

Views: 851

Answers (1)

Prakash
Prakash

Reputation: 872

Try the below code, I have modified [String: Any] to NSDictionary. Also Changed for loop.

var _SurveySubmitModel = [SurveySubmitModel]()

_SurveySubmitModel.append(SurveySubmitModel(ID: 0, SurveyID: 25, QuestionID: 28, FilledBy: 7335, Response: "1In the two weeks before you felt sick, did you:"))
_SurveySubmitModel.append(SurveySubmitModel(ID: 0, SurveyID: 25, QuestionID: 28, FilledBy: 7335, Response: "1In the two weeks before you felt sick, did you:"))
        
for survey in _SurveySubmitModel {
    
    let parameters: NSDictionary = [
        "ID": survey.ID,
        "SurveyID": survey.SurveyID,
        "QuestionID": survey.QuestionID,
        "FilledBy": survey.FilledBy,
        "Response": survey.Response
    ]
    
    Dict.append(parameters)
    
}
print("Dict == ", Dict)

Output is

Dict ==  [{
    FilledBy = 7335;
    ID = 0;
    QuestionID = 28;
    Response = "1In the two weeks before you felt sick, did you:";
    SurveyID = 25;
}, {
    FilledBy = 7335;
    ID = 0;
    QuestionID = 28;
    Response = "1In the two weeks before you felt sick, did you:";
    SurveyID = 25;
}]

Try below function to web service call

func postValues(requestParams: [[String: AnyObject]], urlString: String) {

    let url = URL(string: urlString)
    var request = URLRequest(url: url!)
    request.setValue("application/json", forHTTPHeaderField: "Content-Type")
    request.httpMethod = "POST"
    request.httpBody = try! JSONSerialization.data(withJSONObject: requestParams, options: [])

    AF.request(request).responseJSON { (response) in
        switch response.result {
        case .success:
          //  print(response.result.value)
            break
        case .failure:
            print(response.error)
            break
        }
    }
}

Upvotes: 2

Related Questions