我正在尝试根据使用 swift 库(https://github.com/ankurp/Dollar)的 方法从数组(称为 )中提取变量( 等),safety_rating_id
以便以后可以发出 POST 请求(最终,我希望能够用提取的数组变量替换参数中的硬编码值),但我的结果全部返回 nil。score
array
submission_id
Dollar
find
find
这些是我想要的内部数组,其中submission_id
来自27
该数组https://codeshare.io/zr1pw(第 22-36 行):
{
"submission_id" : "27",
"name" : "Equipment",
"task_id" : "37",
"points" : "10",
"safety_rating_id" : 105,
"score" : "9"
}, {
"submission_id" : "27",
"name" : "Emergency Equipment",
"task_id" : "37",
"points" : "10",
"safety_rating_id" : 106,
"score" : "9"
}
代码:
var array: [JSON] = []
func submitScore(onCompletion: () -> (), onError: ((NSError) -> ())? = nil) {
guard let endPoint = Data.sharedInstance.submitScoreEndpoint
else { print("Empty endpoint"); return }
let user = Users()
let test = $.find(self.array, callback: { $0 == 27 })
print(test)
let Auth_header = [
"Authorization" : user.token,
]
let parameters: [String:Array<[String:Int]>] = [
"ratings" : [
[
"safety_rating_id" : 105,
"schedule_job_id" : 18,
"score" : 9,
"submission_id" : 27
],
[
"safety_rating_id" : 106,
"schedule_job_id" : 18,
"score" : 10,
"submission_id" : 27
]
]
]
Alamofire.request(.POST, endPoint, headers: Auth_header, parameters: parameters, encoding: .JSON)
.validate()
.responseJSON {
response in
switch response.result {
case .Success(let data):
let json = JSON(data)
print(json)
onCompletion()
case .Failure(let error):
print("Request failed with error: \(error)")
onError?(error)
}
}
}
更新
我能够获得具有submission_id
27 的数组,但我想从两个数组中删除name
并添加一个我从其他地方获得的数组。task_id
submission_id : 27
schedule_job_id
我尝试使用for in
循环从我想要的变量创建我自己的数组,但我一直在nil
崩溃。这是新数组的样子https://codeshare.io/3VJSo
最终我想做一个"ratings" : [chosenArray]