包含控制流语句的闭包不能与结果生成器一起使用 'ViewBuilder'
Closure containing control flow statement cannot be used with result builder 'ViewBuilder'
我真的是编码方面的新手,我正在尝试使用 swiftSoup 但是当我输入代码时它给了我这个错误:(包含控制流语句的闭包不能与结果生成器一起使用 'ViewBuilder')我如果我在错误的地方输入代码或者我忘记了什么,请不要这样做!
enter image description here
这是代码
import SwiftUI
import SwiftSoup
struct ContentView: View {
var body: some View {
do {
let html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>"
let doc: Document = try SwiftSoup.parse(html)
let p: Element = try doc.select("title").first()!
rint(p)
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
不幸的是,您的代码有点混乱。
你在哪里看到这个:
struct ContentView: View {
var body: some View {
// Some Stuff here
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
即 SwiftUI 代码试图为您的应用程序创建用户界面。不幸的是,它不像“正常”Swift 代码。幕后发生的一些事情使创建用户界面变得容易,但如果您是编程新手,则很难理解。
你把你的“普通旧代码”放在视图声明的中间,编译器很困惑地看到它。
相反,让我们将您的代码放入一个函数中。然后就可以调用函数了。
struct ContentView: View {
var body: some View {
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
func parseSomeHTML() {
do {
let html = """
<html>
<head>
<title>First parse</title>
</head>"
<body>
<p>Parsed HTML into a doc.</p>
</body>
</html>
"""
let doc: Document = try SwiftSoup.parse(html)
let p: Element = try doc.select("title").first()!
print(p)
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
}
现在您的代码存在于一个普通的 Swift 函数中。但是你需要从某个地方调用它。让我们添加一个按钮来调用您的函数。将 contentView 更改为:
struct ContentView: View {
var body: some View {
Button("Push Me", action: { parseSomeHTML() })
}
}
现在当您 运行 您的应用程序时,您应该有一个按钮,按下该按钮应该调用 parseSomeHTML
函数。
(请注意我是如何使用三重双引号 (""") 与您的 HTML 一起格式化多行字符串的。这不是必需的,您应该可以使用,但它更漂亮)
我真的是编码方面的新手,我正在尝试使用 swiftSoup 但是当我输入代码时它给了我这个错误:(包含控制流语句的闭包不能与结果生成器一起使用 'ViewBuilder')我如果我在错误的地方输入代码或者我忘记了什么,请不要这样做!
enter image description here
这是代码
import SwiftUI
import SwiftSoup
struct ContentView: View {
var body: some View {
do {
let html = "<html><head><title>First parse</title></head>"
+ "<body><p>Parsed HTML into a doc.</p></body></html>"
let doc: Document = try SwiftSoup.parse(html)
let p: Element = try doc.select("title").first()!
rint(p)
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
不幸的是,您的代码有点混乱。
你在哪里看到这个:
struct ContentView: View {
var body: some View {
// Some Stuff here
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
即 SwiftUI 代码试图为您的应用程序创建用户界面。不幸的是,它不像“正常”Swift 代码。幕后发生的一些事情使创建用户界面变得容易,但如果您是编程新手,则很难理解。
你把你的“普通旧代码”放在视图声明的中间,编译器很困惑地看到它。
相反,让我们将您的代码放入一个函数中。然后就可以调用函数了。
struct ContentView: View {
var body: some View {
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
func parseSomeHTML() {
do {
let html = """
<html>
<head>
<title>First parse</title>
</head>"
<body>
<p>Parsed HTML into a doc.</p>
</body>
</html>
"""
let doc: Document = try SwiftSoup.parse(html)
let p: Element = try doc.select("title").first()!
print(p)
} catch Exception.Error(let type, let message) {
print(message)
} catch {
print("error")
}
}
现在您的代码存在于一个普通的 Swift 函数中。但是你需要从某个地方调用它。让我们添加一个按钮来调用您的函数。将 contentView 更改为:
struct ContentView: View {
var body: some View {
Button("Push Me", action: { parseSomeHTML() })
}
}
现在当您 运行 您的应用程序时,您应该有一个按钮,按下该按钮应该调用 parseSomeHTML
函数。
(请注意我是如何使用三重双引号 (""") 与您的 HTML 一起格式化多行字符串的。这不是必需的,您应该可以使用,但它更漂亮)