2016-10-01 58 views
0

映射我有一個通用的功能:通用功能JSON對象與ObjectMapper

func toObjectMapper<T: Mappable>(mapper: T, success: (result: Mappable) -> Void, failure: (error: NSError) -> Void){ 
     let alomofireApiRequest = AlamofireApiRequest(apiRequest: self) 
     Alamofire.request(alomofireApiRequest) 
      .responseObject { (response: Response<T, NSError>) in 
       guard let value = response.result.value else { 
        failure(error: response.result.error!) 
        return 
       } 
       success(result: value) 
     } 

    } 

而且我想這樣稱呼它:

public func login(login: String, password: String) -> UserResponse { 
     let params = ["email":login, "password":password] 

     let request = ApiRequest(method: .POST, path: "login", parameters: params) 

     request.toObjectMapper(UserResponse.self, success: { result in 
       print(result) 

      }, failure: { error in 
       print(error.description) 
     }) 
    } 

但我總是得到這樣的錯誤:

​​

這是我的userResponse:

import Foundation 
import ObjectMapper 
import RealmSwift 

public class UserResponse: Object, Mappable { 
    dynamic var id = 0 
    dynamic var name = "" 
    dynamic var address = "" 
    dynamic var zipcode = "" 
    dynamic var city = "" 
    dynamic var country = "" 
    dynamic var vat = "" 
    dynamic var email = "" 
    dynamic var created_at = NSDate() 
    dynamic var updated_at = NSDate() 

    override public static func primaryKey() -> String? { 
     return "id" 
    } 

    //Impl. of Mappable protocol 
    required convenience public init?(_ map: Map) { 
     self.init() 
    } 

    public func mapping(map: Map) { 
     id <- map["id"] 
     name <- map["name"] 
     address <- map["address"] 
     zipcode <- map["zipcode"] 
     city <- map["city"] 
     country <- map["country"] 
     vat <- map["vat"] 
     email <- map["email"] 
     created_at <- map["created_at"] 
     updated_at <- map["updated_at"] 


    } 
} 

任何幫助?

回答

0

我認爲問題在於你試圖使用UserResponse作爲實例化對象,但使用UserResponse.self只是類類型。

一個解決方案是讓UserResonse單身(或轉給「toObjectMapper」作爲一個參數之前只是實例化一個實例)

我不知道如果這個代碼具體工作,但它沿着這些路線是: -

public class UserResponse: Object, Mappable { 
dynamic var id = 0 
dynamic var name = "" 
dynamic var address = "" 
dynamic var zipcode = "" 
dynamic var city = "" 
dynamic var country = "" 
dynamic var vat = "" 
dynamic var email = "" 
dynamic var created_at = NSDate() 
dynamic var updated_at = NSDate() 

static let shared = UserResponse() //singleton instantiation 

override public static func primaryKey() -> String? { 
    return "id" 
} 

//Impl. of Mappable protocol 
required convenience public init?(_ map: Map) { 
    self.init() 
} 

public func mapping(map: Map) { 
    id <- map["id"] 
    name <- map["name"] 
    address <- map["address"] 
    zipcode <- map["zipcode"] 
    city <- map["city"] 
    country <- map["country"] 
    vat <- map["vat"] 
    email <- map["email"] 
    created_at <- map["created_at"] 
    updated_at <- map["updated_at"] 


} 
} 

,然後在你的函數調用

public func login(login: String, password: String) -> UserResponse { 
    let params = ["email":login, "password":password] 

    let request = ApiRequest(method: .POST, path: "login", parameters: params) 

    request.toObjectMapper(UserResponse.shared, success: { result in 
      print(result) 

     }, failure: { error in 
      print(error.description) 
    }) 
} 
+0

我做了一個快速測試,它關掉了報警。所以你需要實例化一個UserResponse的實例。以上只是我如何做的一個粗略的例子。 –