简体   繁体   English

获取时出错:“无法转换类型&#39;NSFetchRequest的值 <NSManagedObject> &#39;到预期的参数类型&#39;NSFetchRequest <NSFetchRequestResults> “”

[英]Error in Fetch: “Cannot convert value of type 'NSFetchRequest<NSManagedObject>' to expected argument type 'NSFetchRequest<NSFetchRequestResults>'”

Using this function to sort my cells in my table view. 使用此函数在表视图中对单元格进行排序。 I am getting an error in my fetch request. 我在获取请求中收到错误。 The line inside of the do loop, notes = try context.fetch(request) is causing the error , the request is underlined do循环内部的行, notes = try context.fetch(request)导致错误,请求带有下划线

The error says "Cannot convert value of type 'NSFetchRequest' to expected argument type 'NSFetchRequest'" 错误显示“无法将类型'NSFetchRequest'的值转换为预期的参数类型'NSFetchRequest'”

My TableViewController file 我的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)")
        }


    }


}

You need to cast it as an array of Notes 您需要将其转换为Notes数组

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

Try this: 尝试这个:

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

Of course, I'm assuming your Note class is a subclass of NSManagerObject . 当然,我假设你的Note类是NSManagerObject的子NSManagerObject

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 无法转换&#39;NSFetchRequest类型的值 <NSFetchRequestResult> &#39;到指定类型&#39;NSFetchRequest <T> “ - Cannot convert value of type 'NSFetchRequest<NSFetchRequestResult>' to specified type 'NSFetchRequest<T>' 无法使用类型为((NSFetchRequest <NSFetchRequestResult> )” - Cannot invoke 'fetch' with an argument list of type '(NSFetchRequest<NSFetchRequestResult>)' 无法将“字符串”类型的值转换为预期的参数类型“ NSManagedObject” Swift - Cannot convert value of type 'String' to expected argument type 'NSManagedObject' Swift 尝试保存数组时CoreData出错。 &#39;无法将&#39;String&#39;类型的值转换为预期的参数类型&#39;NSManagedObject&#39;&#39; - Error in CoreData when trying to save an array. 'Cannot convert value of type 'String' to expected argument type 'NSManagedObject'' 转换 NSFetchRequest 时出错<t>到 NSFetchRequest<nsfetchrequestresult> 其中 T 是通用 NSManagedObject</nsfetchrequestresult></t> - Error casting NSFetchRequest<T> to NSFetchRequest<NSFetchRequestResult> where T is generic NSManagedObject 无法转换'String!'类型的值预期的参数类型错误 - Cannot convert value of type 'String!' to expected argument type error 无法将类型&#39;(_)-&gt;()&#39;的值转换为预期的参数类型&#39;((Bool,Error?)-&gt; Void)? - Cannot convert value of type '(_) -> ()' to expected argument type '((Bool, Error?) -> Void)?' 无法将类型“ [AnyObject]”的值转换为预期的参数类型“ [MKAnnotation]”错误 - Cannot convert value of type '[AnyObject]' to expected argument type '[MKAnnotation]' error 错误:无法转换类型“URL?”的值到预期的参数类型'URLRequest - Error : Cannot convert value of type 'URL?' to expected argument type 'URLRequest 无法转换期望参数类型GTLServiceCompletionHandler的值 - Cannot convert value of expected argument type GTLServiceCompletionHandler
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM