JSON and flatMap - error Computed property must have an explicit type

164 views Asked by At

I am having a difficulty with flatMap on returned json data. How can I Implement flatMap in the following code. I keep receiving an error: Computed property must have an explicit type

let task = URLSession.shared.dataTask(with: request) { data, response, error in

    DispatchQueue.main.async {

       if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode == 200 {

            do {
                let json = try JSONSerialization.jsonObject(with: data!, options: []) as? [String: Any]

                if let actors = json?["response"] as? [[String : Any]]  {
                    self.arrayActors = actors.flatMap(Actor.init)
                }

                self.tableView.reloadData()

            } catch {
                print("catch error")
            }


    } //end of if let httpStatus


}//end dispatch

}//end of task

The struct 'Actor' is here:

struct Actor {
    let actorName: String

}

extension Actor {
    init?(JSON: [String : Any]) {
        guard let actorName = JSON["actorName"] as? String else {
            return nil
        }
        self.actorName = actorName   
    }
}

Any help would be greatly appreciated!

1

There are 1 answers

0
Luke On

I just changed it to var actorsArray: [Actor] = [] and changed self.actorsArray = actors.flatMap(Job.init) and I worked!!! Thanks a lot @OOPer!!!