以下代码成功构建
class mNoxHttpClient {
var apiUrl : String = ""
var operation : Int = -1
var postData : AnyObject
var responseData : Forecast?
init (apiUrl:String, operation:Int, postData:AnyObject) {
self.apiUrl = apiUrl
self.operation = operation
self.postData = postData
}
func doInBackground() {
let fullURL = God.getFullURL(apiURL: self.apiUrl)
Alamofire.request(fullURL).responseObject { (response: DataResponse<Forecast>) in
self.responseData = response.result.value!
}
}
func doAfterBackgroundCompletes() {
assert(false, "This method must be overriden by the subclass")
}
//
// Run background task
//
func execute() {
DispatchQueue.global(qos: .background).async {
self.doInBackground()
DispatchQueue.main.async {
self.doAfterBackgroundCompletes()
}
}
}
}
如果我进行以下更改
var responseData AnyObject
我得到了
无法将类型(DataResponse) - &gt;()的值转换为预期的参数类型(DataResponse) - &gt; Void
需要派生类在运行时将typecast
responseData赋予自己的类型。
我怎样才能做到这一点?
修改
我将代码修改为这样。基本上我放弃了responseObject,现在我正在尝试使用responseJSON。我将使用一个objectmapper将json转换为swift pojo。
var responseData : Any? {
get {
return self.responseData
}
set {
self.responseData = newValue
}
}
func doInBackground() {
//
// This method will send the request, and make the response ready
//
let fullURL = God.getFullURL(apiURL: self.apiUrl)
Alamofire.request(fullURL).responseJSON{ response in
self.responseData = response.result.value
}
}