2016-12-15 96 views
0

我是新雨燕3.这里是我的情况:swift3核心数据倒序

我在核心数据保存的一些数据。像1,2,3,4,5...我想推动他们到tableView以相反的顺序(5,4,3,2,1)

下面是我的一些代码:

记录:

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

推入细胞

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 
} 

我想是这样

Array.reversed() 

但Xcode的告诉我结果是联合国用过的。请帮帮我。谢谢。

+1

您也可以在''fetchRequest''中添加'NSSortDescriptor'。 – shallowThought

回答

1

reversed不是变异的方法,它会以相反的顺序返回对象,所以你需要使用那个返回数组。

array = array.reversed() 

注:如果属性名称以小写开始它是更好的。