从存储的抛出函数抛出错误
我有一个抛出完成块处理程序的数组:
typealias CompletionBlock = (MyType) throws -> Void
private var handlers: [CompletionBlock] = []
我希望稍后能够调用它们并抛出这样的错误:
for handler in handlers {
handler(throw myError)
}
但是,这不起作用,因为它期望我将 MyType
传递给处理程序,而不是错误。我该怎么做?
I have an array of throwing completion block handlers:
typealias CompletionBlock = (MyType) throws -> Void
private var handlers: [CompletionBlock] = []
I want to be able to recall them later on and throw an error like this:
for handler in handlers {
handler(throw myError)
}
However, that doesn't work because it's expecting me to pass MyType
to the handler, not an error. How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
你的问题有点模糊,但我假设你的意思是你希望处理程序接受抛出函数的结果。这是可能的,我将展示如何实现,但这并不是您想要的。
“抛出函数的结果”可以通过调用一个调用抛出函数的函数来表达:
以您描述的方式抛出错误:
处理程序将如下所示:
但这不是一个很好的设计。最重要的是它多次执行
result
。相反,您希望传递抛出函数的Result
:更常见的做法是:
您还可以使用
try result.get() 将 Result 转换回 throws
。因此,您可以轻松地在 Result 和 throws 之间移动,无论您喜欢什么地方。结果最适合保存。投掷最适合跟注。你可以两者兼得。(当然,您还应该展望未来并探索用 async/await 替换完成处理程序,但有很多原因您可能不会立即这样做,因此 Result 将在相当长的一段时间内成为一个重要工具。)
Your question is a bit vague, but I'm going to assume you mean that you want the handlers to accept the result of a throwing function. That's possible, and I'll show how, but it's not quite what you want.
"The result of a throwing function" can be expressed by calling a function that calls a throwing function:
Throwing an error in the way you've described like this:
A handler would then look like:
But this is not a great design. Most importantly it executes
result
many times. Instead you want to pass theResult
of a throwing function:The more common look for this is along these lines:
You can also convert back from Result to a throws using
try result.get()
. So you can easily move between Result and throws wherever you like. Result is best for storing. Throws is best for calling. You can have both.(And of course, you should also be looking to the future and exploring replacing completion handlers with async/await, but there are many reasons you might not do that right away, so Result will be an important tool for quite some time.)
您试图通过强制存储闭包抛出错误(无论其输入如何)来更改它的行为。你不能那样做。
您将 CompletionBlock 标记为 throws 的事实仅意味着闭包本身可能会抛出异常。但是,您不能强制它抛出,因为您只能将
MyType
类型的输入参数传递给它。此外,强制抛出闭包是没有意义的,因为无论如何您都需要处理从调用站点抛出的错误。所以你可以简单地从调用站点抛出错误。
或者,如果您需要闭包本身来处理
错误
,则应该更改闭包以接受Result
,并防止注入中的.failure
,闭包本身可能会抛出异常。You are trying to change the behaviour of a stored closure by forcing it to throw an error regardless of its input. You cannot do that.
The fact that you marked
CompletionBlock
asthrows
only means that the closure itself might throw. However, you cannot force it to throw, since you can only pass an input argument of typeMyType
to it.Moreover, there's no point in enforcing a closure to throw, since you need to handle the error thrown from the call-site anyways. So you can just simply throw the error from the call site.
Alternatively, if you need the closure itself to handle an
error
, you should change the closure to accept aResult
and in case.failure
in injected, the closure itself could throw.