简体   繁体   English

无法使用类型为((NSFetchRequest <NSFetchRequestResult> )”

[英]Cannot invoke 'fetch' with an argument list of type '(NSFetchRequest<NSFetchRequestResult>)'

I have created a helper function to load NSManagedObject s using Swift 3, which supports iOS 9.0+. 我创建了一个辅助函数,使用支持iOS 9.0+的Swift 3加载NSManagedObject

class func loadContext(entityName: String, fetchConfiguration: ((NSFetchRequest<NSManagedObject>) -> Void)?) -> AnyObject? {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    // Fetch requested data
    let dataFetchRequest = NSFetchRequest<NSManagedObject>(entityName: entityName)

    // Configure the fetch request with user parameters
    if fetchConfiguration != nil {
        fetchConfiguration!(dataFetchRequest)
    }

    do {
        return try appDelegate.managedObjectContext.fetch(dataFetchRequest as! NSFetchRequest<NSFetchRequestResult>)
    } catch {
        print("Failed to fetch feed data, critical error: \(error)")
    }

    return nil
}

However I get the following error: 但是我收到以下错误:

Cannot invoke 'fetch' with an argument list of type '(NSFetchRequest<NSFetchRequestResult>)'

I have also tried several variants, such as having let dataFetchRequest = NSFetchRequest<NSFechRequestResult>(entityName: entityName) and they all run into the same error. 我还尝试了几种变体,例如let dataFetchRequest = NSFetchRequest<NSFechRequestResult>(entityName: entityName) ,它们都遇到相同的错误。

This has been answered on my question Swift 3/XCode 8 NSManagedObjectContext.fetch error . 这已在我的问题Swift 3 / XCode 8 NSManagedObjectContext.fetch错误中得到了回答。

The error message XCode is providing is incorrect. XCode提供的错误消息不正确。 The correct fix is to cast your return and your method correctly. 正确的解决方法是正确转换返回值和方法。

managedObjectContext.fetch() returns [Any] . managedObjectContext.fetch()返回[Any] There are three changes you need to make. 您需要进行三项更改。

  1. class func loadContext(entityName: String, fetchConfiguration: ((NSFetchRequest<NSManagedObject>) -> Void)?) -> [NSManagedObject]? {
  2. let dataFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
  3. return try appDelegate.managedObjectContext.fetch(dataFetchRequest) as? [NSManagedObject]

I've submitted a bug report to Apple - https://openradar.appspot.com/radar?id=5049582866137088 我已向Apple提交了错误报告-https: //openradar.appspot.com/radar?id=5049582866137088

The changes were a lot simpler, it seems as though xcode wrongfully inferences the "AnyObject" type when converting code from swift-2 to 3.0. 所做的更改要简单得多,似乎在将代码从swift-2转换为3.0时,xcode似乎错误地推断了“ AnyObject”类型。

For reference this is the fixed function from @dmorrow's solution. 作为参考,这是@dmorrow解决方案中的固定函数。

class func loadContext(entityName: String, fetchConfiguration: ((NSFetchRequest<NSManagedObject>) -> Void)?) -> [NSManagedObject]? {
    let appDelegate = UIApplication.shared.delegate as! AppDelegate
    // Fetch requested data
    let dataFetchRequest = NSFetchRequest<NSManagedObject>(entityName: entityName)

    // Configure the fetch request with user parameters
    fetchConfiguration?(dataFetchRequest)

    do {
        return try appDelegate.managedObjectContext.fetch(dataFetchRequest)
    } catch {
        print("Failed to fetch feed data, critical error: \(error)")
    }

    return nil
}

暂无
暂无

声明:本站的技术帖子网页,遵循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>' 获取时出错:“无法转换类型&#39;NSFetchRequest的值 <NSManagedObject> &#39;到预期的参数类型&#39;NSFetchRequest <NSFetchRequestResults> “” - Error in Fetch: “Cannot convert value of type 'NSFetchRequest<NSManagedObject>' to expected argument type 'NSFetchRequest<NSFetchRequestResults>'” 无法调用参数列表类型 - Cannot Invoke an Argument List type 无法使用类型为[AnyObject]的参数列表调用“ setViewController” - Cannot invoke" 'setViewController' with an argument list of type [AnyObject] 无法使用类型为参数的列表调用“ requestAuthorizationToShareTypes” - Cannot invoke 'requestAuthorizationToShareTypes' with an argument list of type 无法使用类型为参数的列表调用“ bindToPort” - Cannot invoke 'bindToPort' with an argument list of type 无法调用类型为()的参数列表的[Method] - Cannot invoke [Method] with argument list of type ( , ) 无法使用类型为((String)&#39;的参数列表调用&#39;performSelector&#39; - Cannot invoke 'performSelector' with an argument list of type '(String)' 无法使用类型为((NSURL,*:NSString?)&#39;的参数列表调用&#39;*&#39; - Cannot invoke '*' with an argument list of type '(NSURL, *: NSString?)' 无法使用StringLiteralConvertible类型的参数列表调用&#39;init&#39; - Cannot invoke 'init' with argument list of type StringLiteralConvertible
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM