Error in my code

Hi everyone! I was working on a playground in Swift Playground and I got an error. I can't understand what this error means, can anyone help me?

This is my code:

struct Preview: View {
    @State var isPlaying = true
    @State var num1 = 60
    @State var num2 = 150

    var body: some View {
        if isPlaying == true {  // ERROR: Only concrete types such as structs, enums and classes can conform to protocols 
// Required by static method 'buildBlock' where 'C0' = ()
            num1 += 1
            print("\(num1)")
        }
    }
}

Thanks.

Answered by Claude31 in 711314022

What do you want to achieve ?

  • isPlaying is a state var. Where is it changed ?
  • you cannot have the statement num += 1 directly here
  • body must build a view.

Here is a code that works (does it do what you want is another question:

struct Preview: View {
    @State var isPlaying = true
    @State var num1 = 60
    @State var num2 = 150
    @State var moreText = ""  // To see what's going on

    var body: some View {
        Button("Change") {
            isPlaying.toggle()  // State var changes here
        }

        Text("Hello\(moreText)")
            .onChange(of: isPlaying, perform: { _ in
                if isPlaying {
                    num1 += 1
                    print("\(num1)")
                    moreText = ""
                } else { moreText = " log \(num1+1) on change"}
          })

    }
}
Accepted Answer

What do you want to achieve ?

  • isPlaying is a state var. Where is it changed ?
  • you cannot have the statement num += 1 directly here
  • body must build a view.

Here is a code that works (does it do what you want is another question:

struct Preview: View {
    @State var isPlaying = true
    @State var num1 = 60
    @State var num2 = 150
    @State var moreText = ""  // To see what's going on

    var body: some View {
        Button("Change") {
            isPlaying.toggle()  // State var changes here
        }

        Text("Hello\(moreText)")
            .onChange(of: isPlaying, perform: { _ in
                if isPlaying {
                    num1 += 1
                    print("\(num1)")
                    moreText = ""
                } else { moreText = " log \(num1+1) on change"}
          })

    }
}
Error in my code
 
 
Q