gpt4 book ai didi

ios - 打开 UIDocument 时如何显示错误信息?

转载 作者:塔克拉玛干 更新时间:2023-11-02 20:29:07 25 4
gpt4 key购买 nike

我有一个基于文档的 iOS 应用程序,当它第一次打开文档时,主视图 Controller 调用 UIDocument.open

document.open { success in
if success { ... set up UI ... }
else { ??? }
}

这里的问题是,如果 success 为 false,我将无法访问该错误。通常,Apple 的 API 会在这些情况下将可选的 Error 参数传递给回调,但由于某些原因,它们不会在这里。

我发现这个方法可以在我的应用程序的 UIDocument 子类中覆盖:

override func handleError(_ error: Error, userInteractionPermitted: Bool) {

现在在该方法中我遇到了错误,但我无法轻松访问名为document.open 的 View Controller ,我需要提供类似用于显示错误消息的 UIAlertController。此 handleError 方法也在非主线程上调用。

看来我需要通过在实例或全局变量中传递信息来进行协调。因为这看起来比 Apple 的通常设计更尴尬——我希望 Error 在 open 的完成处理程序中可用,我想我可能会遗漏一些东西。

是否有另一种推荐的方法来获取错误对象并向用户显示消息?

最佳答案

罗布

如果你真的想变得“敏捷”,你可以实现一个闭包来做到这一点,而不需要静态/全局变量。

我将从定义一个枚举开始,该枚举对 UIDocument 的 API 调用的成功和失败案例进行建模。通用 Result 枚举是执行此操作的一种非常常见的方法。

enum Result<T> {
case failure(Error)
case success(T)
}

从那里我会在你的类中定义一个可选的闭包来处理 UIDocument.open 的结果

我会做的实现是这样的:

class DocumentManager: UIDocument {

var onAttemptedDocumentOpen: ((Result<Bool>) -> Void)?

func open(document: UIDocument){
document.open { result in
guard result else { return } // We only continue if the result is successful

// Check to make sure someone has set a function that will handle the outcome
if let onAttemptedDocumentOpen = self.onAttemptedDocumentOpen {
onAttemptedDocumentOpen(.success(result))
}
}
}

override func handleError(_ error: Error, userInteractionPermitted: Bool) {
// Check to make sure someone has set a function that will handle the outcome
if let onAttemptedDocumentOpen = self.onAttemptedDocumentOpen {
onAttemptedDocumentOpen(.failure(error))
}
}

}

然后我从任何类将使用 DocumentManager 你会做这样的事情:

class SomeOtherClassThatUsesDocumentManager {

let documentManger = DocumentManager()

let someViewController = UIViewController()

func someFunction(){
documentManger.onAttemptedDocumentOpen = { (result) in
switch result {
case .failure(let error):
DispatchQueue.main.async {
showAlert(target: self.someViewController, title: error.localizedDescription)
}

case .success(_):
// Do something
return
}
}
}
}

奖励:这是我编写的用于在某些 View Controller 上显示 UIAlertController 的静态函数

/** Easily Create, Customize, and Present an UIAlertController on a UIViewController

- Parameters:
- target: The instance of a UIViewController that you would like to present tye UIAlertController upon.
- title: The `title` for the UIAlertController.
- message: Optional `message` field for the UIAlertController. nil by default
- style: The `preferredStyle` for the UIAlertController. UIAlertControllerStyle.alert by default
- actionList: A list of `UIAlertAction`. If no action is added, `[UIAlertAction(title: "OK", style: .default, handler: nil)]` will be added.

*/
func showAlert(target: UIViewController, title: String, message: String? = nil, style: UIAlertControllerStyle = .alert, actionList: [UIAlertAction] = [UIAlertAction(title: "OK", style: .default, handler: nil)] ) {
let alert = UIAlertController(title: title, message: message, preferredStyle: style)
for action in actionList {
alert.addAction(action)
}
// Check to see if the target viewController current is currently presenting a ViewController
if target.presentedViewController == nil {
target.present(alert, animated: true, completion: nil)
}
}

关于ios - 打开 UIDocument 时如何显示错误信息?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51548675/

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