' to expected argument type ' NSFetchRequest'"-6ren"> ' to expected argument type ' NSFetchRequest'"-使用此函数对表格 View 中的单元格进行排序。我的获取请求中出现错误。 do 循环内的行 notes = try context.fetch(request) 导致错误,请求带有下划线 错误显示“无-6ren">
gpt4 book ai didi

ios - 获取错误: "Cannot convert value of type ' NSFetchRequest' to expected argument type ' NSFetchRequest'"

转载 作者:行者123 更新时间:2023-11-29 05:48:13 25 4
gpt4 key购买 nike

使用此函数对表格 View 中的单元格进行排序。我的获取请求中出现错误。 do 循环内的行 notes = try context.fetch(request) 导致错误,请求带有下划线

错误显示“无法将类型“NSFetchRequest”的值转换为预期参数类型“NSFetchRequest””

我的TableViewController文件

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


}


}

最佳答案

试试这个:

func loadDataFromDatabase() {
...

let request = NSFetchRequest<NSFetchRequestResult>(entityName: "Note")

...

do {
notes = try context.fetch(request) as? [Note] ?? []
} catch let errer as NSError {
print("Could not fetch. \(error), \(error.userInfo)")
}
}

当然,我假设您的 Note 类是 NSManagerObject 的子类。

关于ios - 获取错误: "Cannot convert value of type ' NSFetchRequest<NSManagedObject >' to expected argument type ' NSFetchRequest<NSFetchRequestResults >'",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55943334/

25 4 0