I am new of Swift 3. Here is my situation:
I saved some data in the core data. Like 1,2,3,4,5...
I want to push them into the tableView
with reversed order (5,4,3,2,1)
.
Here are some of my code:
Records:
override func viewWillAppear(_ animated: Bool) {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let managedContext = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSFetchRequestResult>(entityName: "List")
do {
let result = try managedContext.fetch(request)
Array = result as! [NSManagedObject]
}
catch {
print("some error.....")
}
}
Push into cell
override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "MyCell", for: indexPath as IndexPath)
let item = Array[indexPath.row]
cell.textLabel?.text = item.value(forKey: "data") as? String
return cell
}
I tried something like
Array.reversed()
But the Xcode told me that the results are unused. Please help me. Thanks.
reversed
is not mutating method, it will return object in reversed order so you need to use that return array.
array = array.reversed()
Note: It is better if property name start with lower case.