尝试以正确的方式处理错误和可选项
Trying to do deal with errors and optionals the right way
我正在尝试使用 SwiftSoup 来抓取一些 HTML。此示例基于 SwiftSoup github 文档,运行良好……
func scrape() throws {
do {
let htmlFromSomeSource = "<html><body><p class="nerp">HerpDerp</p><p class="narf">HoopDoop</p>"
let doc = try! SwiftSoup.parse(htmlFromSomeSource)
let tag = try! doc.select("p").first()!
let tagClass = try! tag.attr("class")
} catch {
print("oh dang")
throw Abort(.notFound)
}
print(tagClass)
}
… 直到我弄乱了选择器或属性目标,这时一切都崩溃了,这要归功于隐式解包的可选值(我认为这只是让更聪明的人开始的快速而肮脏的代码)。 do/catch 似乎一点用都没有。
那么正确的方法是什么?这编译...
print("is there a doc?")
guard let doc = try? SwiftSoup.parse(response.body.description) else {
print("no doc")
throw Abort(.notFound)
}
print("should halt because there's no img")
guard let tag = try? doc.select("img").first()! else {
print("no paragraph tag")
throw Abort(.notFound)
}
print("should halt because there's no src")
guard let tagClass = try? tag.attr("src") else {
print("no src")
throw Abort(.notFound)
}
...但如果我弄乱了选择器或属性,它又会崩溃,"Unexpectedly found nil while unwrapping an Optional value"(在 "is there a doc?" 之后)。我以为守卫会在遇到 nil 时停止进程? (如果我将 "try?" 转换为 "try",编译器会抱怨 "initializer for conditional binding must have Optional type"...)
如果将函数声明为 throws
,则函数内部不需要 do - catch
块。只需删除 try
之后的块和感叹号即可将错误传递给调用函数。
func scrape() throws { // add a return type
let htmlFromSomeSource = "<html><body><p class="nerp">HerpDerp</p><p class="narf">HoopDoop</p>"
let doc = try SwiftSoup.parse(htmlFromSomeSource)
guard let tag = try doc.select("p").first() else { throw Abort(.notFound) }
let tagClass = try tag.attr("class")
// return something
}
我正在尝试使用 SwiftSoup 来抓取一些 HTML。此示例基于 SwiftSoup github 文档,运行良好……
func scrape() throws {
do {
let htmlFromSomeSource = "<html><body><p class="nerp">HerpDerp</p><p class="narf">HoopDoop</p>"
let doc = try! SwiftSoup.parse(htmlFromSomeSource)
let tag = try! doc.select("p").first()!
let tagClass = try! tag.attr("class")
} catch {
print("oh dang")
throw Abort(.notFound)
}
print(tagClass)
}
… 直到我弄乱了选择器或属性目标,这时一切都崩溃了,这要归功于隐式解包的可选值(我认为这只是让更聪明的人开始的快速而肮脏的代码)。 do/catch 似乎一点用都没有。
那么正确的方法是什么?这编译...
print("is there a doc?")
guard let doc = try? SwiftSoup.parse(response.body.description) else {
print("no doc")
throw Abort(.notFound)
}
print("should halt because there's no img")
guard let tag = try? doc.select("img").first()! else {
print("no paragraph tag")
throw Abort(.notFound)
}
print("should halt because there's no src")
guard let tagClass = try? tag.attr("src") else {
print("no src")
throw Abort(.notFound)
}
...但如果我弄乱了选择器或属性,它又会崩溃,"Unexpectedly found nil while unwrapping an Optional value"(在 "is there a doc?" 之后)。我以为守卫会在遇到 nil 时停止进程? (如果我将 "try?" 转换为 "try",编译器会抱怨 "initializer for conditional binding must have Optional type"...)
如果将函数声明为 throws
,则函数内部不需要 do - catch
块。只需删除 try
之后的块和感叹号即可将错误传递给调用函数。
func scrape() throws { // add a return type
let htmlFromSomeSource = "<html><body><p class="nerp">HerpDerp</p><p class="narf">HoopDoop</p>"
let doc = try SwiftSoup.parse(htmlFromSomeSource)
guard let tag = try doc.select("p").first() else { throw Abort(.notFound) }
let tagClass = try tag.attr("class")
// return something
}