我试图从网上获取数据并尝试使用AZTableView库进行分页。我面临上述错误。这是我的代码
我的模态课程
class JobsNearBy: NSObject {
var jobId: Int?
var title: String?
var companyName: String? }
获取数据代码 我第一次从网上获取10行将它们放入对象并追加到数组并返回。
func jobsNearByFetch(pageNumber: Int, success:@escaping (_ status:Bool, _ jobsNearByArray:Any) -> (), failure:@escaping (_ message: Error) -> ()) {
let headers: HTTPHeaders = ["Accept": "application/json",
"Authorization": "Bearer eyJ0eXAiOiJKV1QiLCJhb"]
let url = "http://thedemo.net/demo/stdinaus/api/jobs-near-me?page=\(pageNumber)&latitude=27.6947033&longitude=85.3310636"
Alamofire.request(url, headers: headers).responseJSON { response in
guard let jobsResponse = response.result.value as? [String:Any] else{
print("Error: \(String(describing: response.result.error))")
failure((response.result.error! as Error))
return
}
// print("response: \(jobsResponse)")
let jobsNearByObj:JobsNearBy = JobsNearBy()
var jobsNearByArray:Array = [JobsNearBy]()
let dict = jobsResponse as NSDictionary
let status = dict["status"] as? Int
let meta = dict["meta"] as! NSDictionary
let lastPage = meta["last_page"] as? Int
let dataArray = dict["data"] as! NSArray
for dataDict in dataArray{
let dataCompanyName = dataDict as! NSDictionary
let jobId = dataDict as! NSDictionary
let title = dataDict as! NSDictionary
if let companyName = dataCompanyName["company_name"],
let jobId = jobId["jobId"],
let title = title["title"]{
jobsNearByObj.companyName = companyName as? String
jobsNearByObj.jobId = jobId as? Int
jobsNearByObj.title = title as? String
jobsNearByArray.append(jobsNearByObj)
}
}
success(true, jobsNearByArray)
}
}
AZTableViewController中的代码
override func fetchData() {
super.fetchData()
if Util.isConnectedToInternet(){
self.showLoading(view: self.view, text: "Loading..")
APIHandler.sharedInstance.jobsNearByFetch(pageNumber: 1, success: { (status, jobsArray) in
self.stopLoading(fromView: self.view)
self.arrayOfJobs.removeAll()
self.arrayOfJobs.append(jobsArray as! JobsNearBy)
self.didfetchData(resultCount: self.arrayOfJobs.count, haveMoreData: true)
}) { (failure) in
self.stopLoading(fromView: self.view)
print("Failure")
}
}else{
Util.showAlert(title:"Oops", message:"No internet connection..", view:self)
}
}
override func fetchNextData() {
super.fetchNextData()
if Util.isConnectedToInternet(){
self.showLoading(view: self.view, text: "Loading..")
APIHandler.sharedInstance.jobsNearByFetch(pageNumber: 2, success: { (status, jobsArray) in
self.stopLoading(fromView: self.view)
self.arrayOfJobs.append(jobsArray as! JobsNearBy)
if self.arrayOfJobs.count < 10{
self.didfetchData(resultCount: self.arrayOfJobs.count, haveMoreData: true)
}else{
self.didfetchData(resultCount: self.arrayOfJobs.count, haveMoreData: false)
}
}) { (failure) in
self.stopLoading(fromView: self.view)
print("Failure")
}
}else{
Util.showAlert(title:"Oops", message:"No internet connection..", view:self)
}
}
我认为我在追加行上犯了错误但无法解决这个问题。请有人帮我解决上述错误。
答案 0 :(得分:0)
jobsNearByFetch
的完成处理程序返回JobsNearBy
数组,您将其放入jobsArray
然后,你有jobsArray
到JobsNearBy
的强制转换,但它是一个数组,而不是对象的单个实例,所以downcast失败,因为它是一个强制downcast你的应用程序崩溃
您可以使用as! [JobsNearBy]
修复它,但最好更改完成闭包的签名,以表明它返回[JobsNearBy]
而非{{1} };然后你不需要贬低任何东西:
当您可以确定实际类型是什么时,您不应该使用Any
。另外,如果你可以避免使用Swift,你就不应该使用Any
。此外,尽可能避免强制向下倾斜和展开。
在风格上,布尔NSDictionary
参数和单独的失败闭包也有点奇怪;您通常会有一个返回可选success
的闭包 - 如果错误为Error
,则操作成功。
我会:
func jobsNearByFetch(pageNumber:Int,completion:@escaping(_ jobsNearByArray:[JobsNearBy]?,错误:错误?) - &gt;()){
这样你可以使用单个尾随闭包。
您还需要查看nil
,因为有一些不会调用关闭的返回路径。
最后,您应该查看Codeble
协议,因为它可以完全消除JSON解析代码。