因为很多人都有此错误,所以我也不知道为什么“不能使用类型为((JSONResponse>)-> _)的参数列表来调用responseObject”
我也在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"]
}
}
最佳答案
Alamofire.request(.GET, "http://yoururl.de")
.response { (request, response, data, error) in
}
基本的alamofire get请求看起来像这样。请求完成后将调用响应。
之后,我建议使用SwiftyJSON并转换您的响应数据。
if let data = (responseBody as NSString).dataUsingEncoding(NSUTF8StringEncoding) {
let json = JSON(data: data)
println(json)
}
关于ios - Alamofire.requestObject自定义案例未调用,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/32096063/