sma*_*8dd 10 exception try-catch guard swift
在swift中,如果进入else块,是否可以使用short guard let try? 并获取发生的异常?
guard let smth = try? myThrowingFunc() else {
print(error) //can I access the exception here somehow?
return
}
Run Code Online (Sandbox Code Playgroud)
VS
let smth: AnyObject?
do {
smth = try myThrowingFunc()
} catch let error {
print(error)
return
}
Run Code Online (Sandbox Code Playgroud)
我在"The Swift Programming Language(Swift 2.2 Prerelease)"中找到了第42页,其中明确说明了以下内容:
处理错误的另一种方法是使用
try?将结果转换为可选的.如果函数抛出错误,则丢弃特定错误,结果为nil.否则,结果是一个可选项,其中包含函数返回的值.
所以,这更像是对苹果的功能请求.事实上,这里已经就这个话题进行了一些讨论:
http://thread.gmane.org/gmane.comp.lang.swift.evolution/8266
| 归档时间: |
|
| 查看次数: |
2134 次 |
| 最近记录: |