gpt4 book ai didi

ios - 多参数字典(集合列表)如: [[String: Any]] to Alamofire Parameters

转载 作者:行者123 更新时间:2023-12-01 16:05:05 25 4
gpt4 key购买 nike

嗨,我正在尝试为字典中名为“Dict”的 alamofire 参数提供...字典可以包含 3 个或 X 个项目。我正在尝试使用 FOR 循环将字典添加到另一组项目中,但是......它只显示最后一个......似乎它覆盖了前一个。我尝试了我所知道的一切......甚至尝试使用 SwiftyJSON 框架......但 alamofire 只采用纯字典类型。

    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)")
好吧,我需要这样的东西
 [{
"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:"
}
]

最佳答案

试试下面的代码,我已经修改了 [String: Any]NSDictionary .也更改了 for环形。

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)

输出是
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;
}]

尝试以下功能来调用 Web 服务
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
}
}
}

关于ios - 多参数字典(集合列表)如: [[String: Any]] to Alamofire Parameters,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/63810824/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com