2017-03-28 58 views
3

注意:我需要使用ObjectMapper,而不是AlamoFireObjectMapper。通过ObjectMapper映射Alamofire responseJSON

您如何映射而不使用JSON,而是从responseJSON数据映射到用户?以下是试图从responseJSON到jsonn,然后再回到数据(是用户)。我猜这肯定有办法吗?

还是应该使用AlamoFire的responseData?

public class User: Mappable { 
    var username: String? 

    required public init?(map: Map) { 

    } 

    // Mappable 
    public func mapping(map: Map) { 
     username <- map["username"] 
    } 
} 


    Alamofire.request(
     url, 
     method: .get, 
     headers: headers_employees).responseJSON { 
      response in 
     // Map via ObjectMapper 
     let [User] = Mapper<User>().map(JSONString: JSONString) 

    } 

回答

2

地图功能还接受JSONObject (Any)JSON dictionary ([String: Any])。根据响应对象的类型,您应该使用适当的方法。

在你的情况使用下面的代码映射JSONResponse

Alamofire.request(
    url, 
    method: .get, 
    headers: headers_employees).responseJSON { 
     response in 
    // Map via ObjectMapper 
    let [User] = Mapper<User>().map(JSONObject:response.result.value) 
}