Home > Enterprise >  Closure containing control flow statement cannot be used with result builder 'ViewBuilder'
Closure containing control flow statement cannot be used with result builder 'ViewBuilder'

Time:11-10

I'm really new in coding and I'm trying to swiftSoup but when I enter the code it gives me this error:(Closure containing control flow statement cannot be used with result builder 'ViewBuilder') I don't if I enter the code in the wrong place or I'm forgetting something!

enter image description here

This is the code

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()
    }
}

CodePudding user response:

Unfortunately, your code is a little confused.

Where you see this:

struct ContentView: View {
    var body: some View {
       // Some Stuff here
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

That is SwiftUI code that is trying to create a user interface for your application. Unfortunately it's not like "normal" Swift code. There are some behind the scenes things going on that make it easy to create user interfaces, but hard to understand if you're new to programming.

You put your "plain old code" in the middle of the view declaration and the compiler is very confused to see it there.

Instead, let's take your code out into a function. Then you can call the function.

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")
    }
}

Now your code lives in a plain, ordinary Swift function. But you're going to need to call it from somewhere. Let's add a button to call your function. Change contentView to this:

struct ContentView: View {
    var body: some View {
        Button("Push Me", action: { parseSomeHTML() })
    }
}

Now when you run your app, you should have a button, and pushing that button should call the parseSomeHTML function.

(Note how I used triple double quotes (""") to format a multi-line string with your HTML. It's not necessary, what you had should work, but it's prettier)

  • Related