[英]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. 您需要进行三项更改。
class func loadContext(entityName: String, fetchConfiguration: ((NSFetchRequest<NSManagedObject>) -> Void)?) -> [NSManagedObject]? {
let dataFetchRequest = NSFetchRequest<NSFetchRequestResult>(entityName: entityName)
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.