-2

I'm having a problem with the following code. I'm downloading a list of actors in JSON and I want to populate Struct Actor with the received data. Everything works great until I try to flatMap on the received data and try to initialize the struct Actor. When I try to compile the code i get the error: Cannot assign value of type '()' to type [Actor]. The error corresponds to a line in viewDidLoad actorsList = downloadActors() Would anybody have any recommendation who to solve this?

import UIKit

func downloadActors() {


var request = URLRequest(url: URL(string: "url...")!)

request.httpMethod = "POST"
let postString = "actorGroup=\("Superhero")"
request.httpBody = postString.data(using: .utf8)


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

DispatchQueue.main.async {

        guard let data = data, error == nil else {
            print("error=\(error)")
            return
        }


if let httpStatus = response as? HTTPURLResponse, httpStatus.statusCode != 200 {
            print("error : statusCode should be 200 but is \(httpStatus.statusCode)")
            print("response = \(response)")
        }

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

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

            guard let actorsJSON = json?["response"] as? [[String : AnyObject]]  else {
                    return
                }


            } catch {
                print("catch error")
            }


        }
}

  }

    task.resume()

}

func loadActors() -> [Actor] {
                        if let actors = actorsJSON as? [[String : AnyObject]] {
                            return actors.flatMap(Actor.init)

                        }
                    }
                    let  actorsArray = loadActors()



class MasterViewController: UITableViewController {

    var actorsList = [Actor]()

    var detailViewController: DetailViewController? = nil
    var objects = [Any]()


    override func viewDidLoad() {

        super.viewDidLoad()

        actorsList = downloadActors()


       print(actorsList)

Struct Actors is as follows:

struct Job {

    let actorGroup: String
    let actorName: String

}

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

        }
    }
Luke
  • 407
  • 2
  • 10
  • 22
  • 1
    Very confusing code. What does the function in the middle of the `do` block? Why do you type-check `actorsJSON` twice? The computed property is `let listActors...` which should be probably an optional binding (`if let ...` ). Further `.mutableContainers` is completely nonsense in Swift. And finally a JSON dictionary is `[String:Any]` in Swift 3. – vadian Jan 02 '17 at 17:04
  • Where the `func loadActors` should be located? if i place it out of the `do` block then I get an error 'unresolved identifier' – Luke Jan 02 '17 at 17:56
  • Actually you don't need the function wrapper at all. – vadian Jan 02 '17 at 17:58
  • were you able to resolve the issue, as vadian said, there are lots of issues/imperfection in your code. – Bista Jan 03 '17 at 09:53

1 Answers1

0
let listActors = actorsJSON as? [[String : AnyObject]] {

Should be:

if let listActors = actorsJSON as? [[String : AnyObject]] {

Edit: For more info I'd like to add Vadian's comment:

Very confusing code. What does the function in the middle of the do block? Why do you type-check actorsJSON twice? The computed property is let listActors... which should be probably an optional binding (if let ... ). Further .mutableContainers is completely nonsense in Swift. And finally a JSON dictionary is [String:Any] in Swift 3.

Community
  • 1
  • 1
Bista
  • 7,869
  • 3
  • 27
  • 55