我在swift3中弄錯了錯誤處理。我嘗試做像「如果XX功能得到了錯誤,那麼嘗試YY功能」 讓我告訴你,我嘗試:在Swift 3中對錯誤處理感到困惑
class MyClass {
enum error: Error
{
case nilString
}
func findURL() {
do {
let opt = try HTTP.GET(url_adr!)
opt.start { response in
if let err = response.error {
print("error: \(err.localizedDescription)")
return //also notify app of failure as needed
}
do
{
/* This is func1. and got error. I want to if this function has error then go next function. */
try self.stringOperation(data: response.description)
}
catch{
print("doesn't work on func1. trying 2nd func")
self.stringOperation2(data:response.descritption)
}
}
} catch let error {
print("got an error creating the request: \(error)")
}
}
func stringOperation(data:String)throws -> Bool{
do{
/** 1 **/
if let _:String = try! data.substring(from: data.index(of: "var sources2")!){
print("its done")
}else{
throw error.nilString
}
IN 1:我在這一行這種致命的錯誤: 「致命錯誤:意外發現零,同時展開一個可選值「和程序崩潰。 我使用Google搜索錯誤處理嘗試理解並應用到我的代碼中。但尚未成功。有人能解釋我在哪裏錯了嗎?
附加信息:我得到了.substring(from:...)和.index(of:「str」)的String擴展。所以這些線不會讓你感到困惑。
給出行號,程序崩潰或找到零 –
錯誤行低於「/ ** 1 ** /」消息...但tbogosia的答案已解決...謝謝 – Antiokhos