作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
嗨,我正在尝试为字典中名为“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/
我是一名优秀的程序员,十分优秀!