I am trying to pull out variables (safety_rating_id
, score
etc.) from an array (called array
) based on the submission_id
using the swift library Dollar
(https://github.com/ankurp/Dollar)'s find
method so that I can later make a POST request (Eventually, I hope to be able to replace the hardcoded values in my parameter with the pulled array variables) with them but my results from find
all return nil.
These are the inner arrays I want where the submission_id
is 27
from this array https://codeshare.io/zr1pw (lines 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"
}
Code:
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)
}
}
}
UPDATE
I'm able to obtain the array with submission_id
27 BUT I want to remove name
and task_id
from the two submission_id : 27
arrays AND add an schedule_job_id
that I got from elsewhere.
I've tried using a for in
loop to create my own array from the variables that I want but I keep getting a nil
crash. This is what the new array looks like https://codeshare.io/3VJSo
Eventually I want to do a "ratings" : [chosenArray]
.validate()
twice? – Aragonite