gpt4 book ai didi

swift - 获取请求时返回错误?

转载 作者:行者123 更新时间:2023-11-30 12:50:48 25 4
gpt4 key购买 nike

我正在尝试弄清楚如何返回 do/catch 语句的错误部分,因为它不在我没有完成发送到的函数中,并且它只想接受 NSFetchedResultsController ?

这是代码

    fileprivate lazy var fetchedExercisesTodayController: NSFetchedResultsController<UserExercise> = {

let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "usersroutine == %@", self.routineName)

do {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let queryResults = try context.fetch(fetchRequest)

fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController

} catch {
print("Error with request: \(error)")
return error
}
}()

错误出现在“返回错误”行

Cannot convert return expression of type 'Error' to return type 'NSFetchedResultsController'

最佳答案

如果它不会失败,则根本不需要 try/catch,因此只需将其删除即可。

fileprivate lazy var fetchedExercisesTodayController: NSFetchedResultsController<UserExercise> = {

let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "usersroutine == %@", self.routineName)

let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let queryResults = context.fetch(fetchRequest)

fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController
}()

但是,如果您想防止失败(这可能在这里发生),您可以设置 fetchedExcercisesTodayController一个可选变量。您必须将其设置为可选,因为如果不是,您需要返回 NSFetchedResultsController<UserExercise> 类型的对象无论。错误不是对象类型 NSFetchedResultsController<UserExercise> 。它是对象类型Error

如果您将其设置为可选,则在 catch 中您可以返回 nil 并执行以下三件事之一:

  • 调用另一个方法,在尝试失败时显示/执行某些操作
  • 抛出一个错误,您可以在此之外的地方进行处理
  • 让它为您打印一条错误消息

这样做的唯一问题是,如果您已经初始化并使用此变量,则必须开始解包可选的 fetchedExercisesTodayController您使用过它的每个地方。本质上,这两件事都会起作用,您只需决定什么最适合您的代码库。

//changed to NSFetchedResultsController<UserExercise>?
fileprivate lazy var fetchedExercisesTodayController: NSFetchedResultsController<UserExercise>? = {

let fetchRequest: NSFetchRequest<UserExercise> = UserExercise.fetchRequest()
fetchRequest.predicate = NSPredicate(format: "usersroutine == %@", self.routineName)

do {
let appDelegate = UIApplication.shared.delegate as! AppDelegate
let context = appDelegate.persistentContainer.viewContext
let queryResults = try context.fetch(fetchRequest)

fetchRequest.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]
let fetchedResultsController = NSFetchedResultsController(fetchRequest: fetchRequest, managedObjectContext: self.persistentContainer.viewContext, sectionNameKeyPath: nil, cacheName: nil)
fetchedResultsController.delegate = self
return fetchedResultsController

} catch {
print("Error with request: \(error)")
//handle error in some way here
return nil
}
}()

关于swift - 获取请求时返回错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41006910/

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