我在对Swift中的表格视图单元进行排序的方法中收到错误。
错误来自行上的func loadDataFromDatabase()
:
let sortDescriptor = NSSortDescriptor(key: sortPriority)
我的表View Controller文件:
import UIKit
import CoreData
class noteTableViewController: UITableViewController {
var notes = [Note]()
var managedObjectContext: NSManagedObjectContext? {
return (UIApplication.shared.delegate as! AppDelegate).persistentContainer.viewContext
}
func loadDataFromDatabase() {
let settings = UserDefaults.standard
let sortPriority = settings.string(forKey: Constants.kPriority)
let context = appDelegate.persistentContainer.viewContext
let request = NSFetchRequest<NSManagedObject>(entityName: "Note")
let sortDescriptor = NSSortDescriptor(key: sortPriority)
let sortDescriptorsArray = [sortDescriptor]
request.sortDescriptors = sortDescriptorsArray
do {
notes = try context.fetch(request)
} catch let errer as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}
}
答案 0 :(得分:0)
您的操作方式是使编译器与init(coder:)
初始化程序混淆。要使用所需的初始化程序,还需要提供ascending
参数:
let sortDescriptor = NSSortDescriptor(key: sortPriority, ascending: true)