gpt4 book ai didi

ios - 无法将类型 'NSFetchRequest' 的值转换为指定类型 'NSFetchRequest'

转载 作者:搜寻专家 更新时间:2023-10-30 23:00:54 26 4
gpt4 key购买 nike

我想使用 iOS10 中引入的新核心数据 API。

@available(iOS 10.0, *)
open class func fetchRequest() -> NSFetchRequest<NSFetchRequestResult>

我创建了通用函数来获取各种对象:

func getAllEntities<T: NSManagedObject>(ofType type: T.Type, success: @escaping ([T]) -> ()) throws -> [T] {
let fetchRequest: NSFetchRequest<T> = T.fetchRequest()
let asyncFetchRequest = NSAsynchronousFetchResult(fetchRequest: fetchRequest) { result in
success(result.finalResult ?? [])
}

try databaseStack.persistentContainer.viewContext.execute(asyncFetchRequest)
}

但符合 let fetchRequest: NSFetchRequest<T> = T.fetchRequest()我有问题:

Cannot convert value of type 'NSFetchRequest<NSFetchRequestResult>' to specified type 'NSFetchRequest<T>'

我做错了什么?

最佳答案

T.fetchRequest()返回 NSFetchRequest<NSFetchRequestResult> ,你必须明确地将它转换为特定的 NSFetchRequest<T> :

let fetchRequest = T.fetchRequest() as! NSFetchRequest<T>
let asyncFetchRequest = NSAsynchronousFetchRequest(fetchRequest: fetchRequest) { result in
success(result.finalResult ?? [])
}

关于ios - 无法将类型 'NSFetchRequest<NSFetchRequestResult>' 的值转换为指定类型 'NSFetchRequest<T>',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48115762/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com