【问题标题】:Alamofire.requestObject custom case not invokingAlamofire.requestObject 自定义案例未调用
【发布时间】:2015-11-12 18:52:09
【问题描述】:

由于很多人都有这个错误,我也有,我不知道为什么“responseObject 不能使用类型为 ((JSONResponse>) -> _) 的参数列表调用”

我也在使用 Alamofire+ObjectMapper 的 JSON 映射

有人知道,Alamofire 有什么问题,经常出错,可能是关于理解 Alamofire 的任何想法?

import Foundation
import Alamofire
import AlamofireObjectMapper
import ObjectMapper

public protocol Mappable {
    static func newInstance(map: Map) -> Mappable?
    mutating func mapping(map: Map)
}

class JMapping {



    func getJSON() {

        let url = "http://www.somestringToJson/json.json"


        Alamofire
        .request(.GET, url, parameters: nil)


            .responseObject { (response: JSONResponse?) in
                println(response?.title)
                if let events = response?.events {
                    for event in events {
                        println(event.id)
                        println(event.title)

                    }
                }
        }
    }

}




class JSONResponse: Mappable {
    var title: String?
    var id: String?
    var events: [EventsResponse]?


    class func newInstance(map: Map) -> Mappable? {
            return JSONResponse()
        }


    func mapping(map: Map) {
        title <- map["title"]
        id <- map["id"]
        events <- map["events"]

    }


}

class EventsResponse: Mappable {
    var title: String?
    var id: String?
    var content: [ContentResponse]?

    class func newInstance(map:Map) -> Mappable? {
        return EventsResponse()
    }

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

}

class ContentResponse: Mappable{
    var content_type: String?
    var visible: Bool?
    var data: NSArray?

    class func newInstance(map: Map) -> Mappable? {
        return ContentResponse()
    }

    func mapping(map: Map) {
        content_type <- map["content_type"]
        visible <- map["visible"]
        data <- map["data"]
    }
}

【问题讨论】:

  • 其实.responseObject { (response: JSONResponse?, error: NSError?) in....也不行
  • 我也试过这个.responseObject { (_, response: JSONResponse?, _, error: NSError?) in

标签: ios json swift mapping alamofire


【解决方案1】:
            Alamofire.request(.GET, "http://yoururl.de")
                    .response { (request, response, data, error) in

                    }

基本的 alamofire 获取请求如下所示。请求完成时调用响应。 之后,我建议使用 SwiftyJSON 并转换您的响应数据。

    if let data = (responseBody as NSString).dataUsingEncoding(NSUTF8StringEncoding) {
        let json = JSON(data: data)
        println(json)
    }

【讨论】:

  • 实际上不是响应,而是响应对象,反正没有运气
猜你喜欢
  • 1970-01-01
  • 2015-11-28
  • 2021-03-10
  • 1970-01-01
  • 2015-09-13
  • 2013-11-05
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多