我的通话错误:通话中的参数标签不正确(具有“键:”,应为“编码器:”)

时间:2019-05-01 21:45:03

标签: ios swift nssortdescriptor

我在对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)")
        }
    }
}

1 个答案:

答案 0 :(得分:0)

您的操作方式是使编译器与init(coder:)初始化程序混淆。要使用所需的初始化程序,还需要提供ascending参数:

let sortDescriptor = NSSortDescriptor(key: sortPriority, ascending: true)