gpt4 book ai didi

swift - 使用 ObjectMapper 映射 JSON 对象的通用函数

转载 作者:行者123 更新时间:2023-11-30 13:02:10 26 4
gpt4 key购买 nike

我有一个通用函数:

   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)
})
}

但我总是收到此错误:

 Cannot invoke 'toObjectMapper' with an argument list of type '(UserResponse.Type, success: (result: Mappable) -> Void, failure: (error: NSError) -> Void)'

这是我的用户响应:

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"]


}
}

有什么帮助吗?

最佳答案

我认为问题在于您尝试使用 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)
})
}

关于swift - 使用 ObjectMapper 映射 JSON 对象的通用函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39804405/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com