使用AlamofireObjectMapper错误

时间:2017-05-30 06:21:34

标签: swift

我使用AlamofireObjectMapper从JSON映射对象,但没有检索到响应。这是我的代码

let articleAPI = "https://news-at.zhihu.com/api/4/news/latest"
Alamofire.request(articleAPI).responseObject {(response: DataResponse<ArticleResponse>) in
        let contents = response.result.value
        print((contents?.stories?[0].title)!)

ArticleResponse对象如下所示:

import ObjectMapper

class ArticleResponse: Mappable {
    var date: String?
    var stories: [ArticleDetailResponse]?
    var top_stories: [TopStoriesResponse]?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        date         <- map["date"]
        stories      <- map["stories"]
        top_stories  <- map["top_stories"]
    }
}

class ArticleDetailResponse: Mappable {
    var images: [String]?
    var type: Int?
    var id: Int?
    var ga_prefix: String?
    var title: String?
    var multipic: Bool?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        images          <-   map["images"]
        type            <-   map["type"]
        id              <-   map["id"]
        ga_prefix       <-   map["ga_prefix"]
        title           <-   map["title"]
        multipic        <-   map["multipic"]
    }
}

class TopStoriesResponse: Mappable {
    var image: String?
    var type: Int?
    var id: Int?
    var ga_prefix: String?
    var title: String?

    required init?(map: Map) {

    }

    func mapping(map: Map) {
        image           <-   map["image"]
        type            <-   map["type"]
        id              <-   map["id"]
        ga_prefix       <-   map["ga_prefix"]
        title           <-   map["title"]
    }
}

我是根据AlamofireObjectMapper用法文档做的。除此之外,还有什么必须实现吗?

0 个答案:

没有答案