我在这个网站上已经多次看到这个问题,但似乎没有一个解决方案可行。
我正在扩展alamofire请求以获得一系列repo对象;但是,我一直在标题中收到错误。这是代码:
extension Alamofire.Request {
class func repoArrayResponseSerializer() -> ResponseSerializer<Array<Repo>, NSError> {
return ResponseSerializer { request, response, data, error in
guard error == nil else { return .Failure(error!) }
guard data != nil else { return .Failure(error!) }
do {
let jsonData: AnyObject? = try NSJSONSerialization.JSONObjectWithData(data!, options: .AllowFragments)
let json = JSON(jsonData!)
if json.error != nil || json == nil {
return .Failure(error!)
}
var repos: Array = Array<Repo>()
for (_, jsonRepo) in json {
let repo = Repo(json: jsonRepo)
repos.append(repo)
}
return .Success(repos)
} catch {
return .Failure(error as NSError)
}
}
}
func responseRepoArray(completionHandler: Result<Array<Repo>, NSError> -> Void) -> Self {
return response(responseSerializer: Request.repoArrayResponseSerializer(), completionHandler: completionHandler)
}
}
感谢任何和所有帮助。