dimanche 4 février 2018

Could not cast value of type 'Swift._ContiguousArrayStorage

I'm trying to fetch data from web and trying paging using AZTableView library. I'm facing the above error. Here's my code

My Modal class

class JobsNearBy: NSObject {
var jobId: Int?
var title: String?
var companyName: String? }

Fetch data code I fetch 10 rows from the web first time put them in object and append on array and return.

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

}

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

I think I've made mistake on append line but unable to solve this. Please someone help me with the above error.




Aucun commentaire:

Enregistrer un commentaire