noobdev noobdev - 6 months ago 10
JSON Question

Cannot pass JSON array to array

I am trying to pass my JSON array to an array called

array
so that I can then query the array with
submission_id
with value 27 to obtain the
safety_rating_id
,
schedule_job_id
,
score
and
submission_id
from this JSON https://codeshare.io/UqJMV but I'm being thrown this error


Cannot convert value of type '[JSON]' to expected argument type 'JSON'


Code to pass JSON to array:

var array: [JSON] = []

func getTask(onCompletion: () -> (), onError: ((NSError) -> ())? = nil) {

guard let endPoint = Data.sharedInstance.weeklyEndpoint
else { print("Empty endpoint"); return }

Alamofire.request(.GET, endPoint, encoding: .JSON)
.validate()
.responseJSON { response in

switch response.result {

case .Success:
if let value = response.result.value {
let json = JSON(value)
for (_,subJson):(String, JSON) in json {
if let date = subJson["start_date"].string{
self.date = date
}
if let building = subJson["building_name"].string{
self.building = building
}
if let jobId = subJson["schedule_job_id"].int {
self.jobIdArray.append(jobId)
}

if let tasks = subJson["tasks"].array{
Tasks.sharedInstance.datas = tasks
for building in tasks {
if let ratings = building["safety_ratings"].array{
print(ratings)

self.array.append(ratings)

}
}
}


}

onCompletion()
}

case .Failure(let error):
print("Request failed with error: \(error)")
onError?(error)
}
}
}

Answer

append() expects a single element (JSON), but ratings is an array ([JSON]).

That's what the error message says.

To append an array use appendContentsOf:

self.array.appendContentsOf(ratings)