swift - 如何快速实现自定义错误抛出语法?
问题描述
我想实现以下内容:
throwingFunction()??.doStuff()
/* if throwingFunction throws an error:
print the error
else
returns an object with the doStuff() Method
*/
throwingFunction()??
/*
if an error is thrown,
prints the error.
else
execute the function without errors.
*/
我不确定在源代码中的何处查找有关如何do, try, catch
实现的示例。Swift 错误文档解释了如何使用已经实现的错误处理方法。需要明确的是,我想使用上述语法实现自定义错误处理。
就像是:
precedencegroup Chaining {
associativity: left
}
infix operator ?? : Chaining
extension Result {
// ERROR: Unary operator implementation must have a 'prefix' or 'postfix' modifier
static func ??(value: Result<Success, Failure>) -> Success? {
switch value {
case .success(let win):
return win
case .failure(let fail):
print(fail.localizedDescription)
return nil
}
}
}
解决方案
推荐阅读
- apache-kafka-connect - ksql表中的重复键
- python - 在尝试训练之前查看我的神经网络的架构
- python - tensorflow.python.framework.errors_impl.UnimplementedError:文件系统方案'gs'未实现
- jquery - Datatable.js:点击数据表中的编辑按钮时页面回发
- python - 在 Django 中,表单提交工作但无法解析用户输入
- postgresql - How do i SELECT multiple Values with postgreSQL INTO multiple variables Depending on a CASE?
- node.js - Node.Js - Return 1 if no matching document is found in MongoDB
- python - 没有管道的工作进程最有效的方法
- asp.net-mvc - 使用页面处理程序部分页面 .net 核心发布表单
- sql - SQL Aggregation with rolling window