2016-12-15 92 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() 

注:如果屬性名稱以小寫開始它是更好的。