使用JSON和flatMap进行结构初始化 [英] Struct Init with JSON and flatMap

查看:434
本文介绍了使用JSON和flatMap进行结构初始化的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码有问题.我正在下载JSON中的actor列表,我想用接收到的数据填充Struct Actor.一切工作都很好,直到我尝试对接收到的数据进行flatMap并尝试初始化struct Actor为止.当我尝试编译代码时,出现错误:无法将类型'()'的值分配给类型[Actor].该错误对应于viewDidLoad actorsList = downloadActors()中的一行,是否有人建议解决此问题?

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

        }
    }

解决方案

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

应该是:

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

有关更多信息,我想添加 Vadian的注释:

非常混乱的代码. 执行阻止中间的功能是什么?为什么要两次检查actorsJSON?计算出的属性是let listActors ...,应该是可选绑定(如果让...).另外,.mutableContainers在Swift中完全是胡说八道.最后,JSON字典在Swift 3中是[String:Any].

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

        }
    }

解决方案

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.

这篇关于使用JSON和flatMap进行结构初始化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆