1

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)
        }
    }      
}

1 Answer 1

1

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)
Sign up to request clarification or add additional context in comments.

4 Comments

How do I filter for the arrays with submission_id = 27 inside? (I want to get the two dictionaries whose submission_id is 27)
let filteredArray = array.filter {$0["submission_id"] as! Int == 27}
it's throwing me a Cast from 'JSON' to unrelated type 'Int' always fails
You need to cast the JSON array to "normal" array probably array.arrayValue.filter...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.