【问题标题】:Swift - Mapping of Nested Objects (Objectmapper)Swift - 嵌套对象的映射 (Objectmapper)
【发布时间】:2018-04-29 06:10:40
【问题描述】:

我正在使用 Swift 4 开发一个应用程序。我使用 Alamofire 调用 APIRest,我想使用 Objectmapper 映射 JSON 响应。嗯,给我回电话的 JSON 如下:

APIRest 的代码是:

    func retrievePostListData() {
        Alamofire
            .request("http://www.speedrun.com/api/v1/games", method: .get)
            .validate()
            .responseArray(completionHandler: { (response: 
            DataResponse<[PostModelSpeedRunModel]>) in
                switch response.result {
                case .success(let posts):

                 self.remoteRequestHandler?.onPostsRetrievedData(posts)
                    case .failure( _):
                        self.remoteRequestHandler?.onError()
                }
            })
    }

问题是我不知道如何访问每个值(函数映射)。因为有一些嵌套值。除此之外,一些通告是对象,而另一些则是数组。我的错误代码如下:

import Foundation
import ObjectMapper

struct PostModelSpeedRunModel {
    var id              = ""
    var international   = ""
    var abbreviation    = ""
    var links           = [Links]??? // I need to get "rel" and "uri" of "runs"
    var uri             = ""
}

extension PostModelSpeedRunModel: Mappable {

    init?(map: Map) {
    }

    mutating func mapping(map: Map) {
        id              <- map["data.id"]
        international   <- map["data.international"]
        abbreviation    <- map["data.abbreviation"]
        link              <- map["data.Links"]
        uri             <- map["data.logo"]
    }

}

你能帮我做/理解做函数映射吗?谢谢

【问题讨论】:

    标签: json swift mapping alamofire objectmapper


    【解决方案1】:

    我假设您根本没有得到任何值,因为我尝试了您的代码但一无所获。如果您只需要 json 中的 data 数组的内容,并且正如您现在所拥有的那样,ObjectMapper 期望一个仅包含 PostModelSpeedRunModels 数组的 json。因此,您需要添加一个keyPath 来告诉AlamofireObjectMapper 它的起点:

        Alamofire.request("http://www.speedrun.com/api/v1/games", method: .get)
            .responseArray(keyPath: "data") { (response: DataResponse<[PostModelSpeedRunModel]>) in
                ...
        }
    

    如果您还需要来自 pagination 节点的信息,那么您需要创建一个具有 datapagination 属性的新对象,并将 responseArray(keyPath: ...) 更改为仅使用 responseObject DataResponse 中的新根对象。

    那么我相信你只想要runs 的uri,所以我建议在你的模型中只使用一个字符串来存储它,而不是一个链接数组。假设链接数组未排序并且将来可能会更改顺序(如果不是,您可以像map["links.1.uri"] 一样直接访问并且您完成了),所有链接都需要解析然后过滤。可以这样做:

    struct PostModelSpeedRunModel {
        var id              = ""
        var international   = ""
        var abbreviation    = ""
        var runsLink        = ""
        var uri             = ""
    }
    
    extension PostModelSpeedRunModel: Mappable {
    
        init?(map: Map) {
        }
    
        mutating func mapping(map: Map) {
            id              <- map["id"]
            international   <- map["international"]
            abbreviation    <- map["abbreviation"]
            uri             <- map["logo"]
    
            var links: [Link]?
            links <- map["links"]
    
            if let uri = links?.first(where: {$0.rel == "runs"})?.uri {
                runsLink = uri
            }
        }
    }
    
    struct Link {
        var rel = ""
        var uri = ""
    }
    
    extension Link: Mappable {
        init?(map: Map) {
        }
    
        mutating func mapping(map: Map) {
            rel <- map["rel"]
            uri <- map["uri"]
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2019-08-03
      • 2020-09-27
      • 2013-08-01
      • 2019-07-13
      • 1970-01-01
      • 1970-01-01
      • 2017-07-20
      • 2018-03-17
      • 2016-12-12
      相关资源
      最近更新 更多