Jarad Kears Jarad Kears - 2 months ago 108
iOS Question

UItableview Could not cast value of type Swift._SwiftDeferredNSArray to NSMutableArray

So I have Two Classes one define mines my cell: ListTableViewCell, and the other one defines my table:ListTableView. I am trying to pull data from a parse server to display as the two uilabels in the cell. I am running into an error at this line of code.

self.Data = array as! NSMutableArray


It is giving me the error "Could not cast value of type 'Swift._SwiftDeferredNSArray' (0x107bc36b8) to 'NSMutableArray' (0x105f7fcd0)."

Any suggestions to fix this.

class ListTableViewCell: UITableViewCell {
@IBOutlet weak var name: UILabel!
@IBOutlet weak var team: UILabel!

}


This is the tableview.

class ListTableViewController: UITableViewController{

var Data: NSMutableArray = NSMutableArray()

func loadData(){

Data.removeAllObjects()

let findData:PFQuery = PFQuery(className: "data")
findData.getFirstObjectInBackground() {(objects: PFObject?, error: Error?) -> Void in
if !(error != nil){
for object in [objects] as [PFObject?]{
self.Data.add(object)
}
}
let array:NSArray = self.Data.reversed() as NSArray
self.Data = array as! NSMutableArray
self.tableView.reloadData()
}

}

override func viewDidAppear(_ animated: Bool) {
self.loadData()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}

override func numberOfSections(in tableView: UITableView) -> Int {
return 1
}

override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return Data.count
}

override func tableView(_ tableView: UITableView?, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell:ListTableViewCell = tableView!.dequeueReusableCell(withIdentifier: "listCell", for: indexPath) as! ListTableViewCell

let listData:PFObject = self.Data.object(at: indexPath.row) as! PFObject

cell.name.text = listData.object(forKey: "name") as? String


return cell
}


}


EDIT***

Changed:

var Data = [PFObject]()


Data.removeAllObjects() which I changed to Data.removeAll()
self.Data.add(object) which I changed to aelf.Data.append(object)


Completely removed this

let array:NSArray = self.Data.reversed() as NSArray
self.Data = array as! NSMutableArray


This last error I can't fix is this one

let listData:PFObject = self.Data.object(at: indexPath.row) as! PFObject


It is throwing an error saying [PFObject] has no member object.

Answer

I think you're having the same issue I had when I started with Swift: You're still using Obj-C arrays. Try using Swift Arrays!

EDIT: To answer your recently edited question, you are referencing objects incorrectly.

What it means when it tells you has no member object is that you are saying:

let listData:PFObject = self.Data.object(at: indexPath.row) as! PFObject

This part specifically: self.data.object(at: indexPath.row)

I'm not sure where you're getting .object(at: ) from but that is incorrect. If you reference the link I shared above, you will see that you reference objects in your swift arrays as such:

let listData:PFObject = self.Data[indexPath.row] as! PFObject