assign value to a bool if contains statement is true

I'd like to make a boolean var true if a string var contains a comma. but keep getting an error

import SwiftUI

struct ContentView: View {   @State var dos = ""   @State var coma : Bool = false       var body: some View {     Form {     TextField("dosis", text: $dos)               if dos.contains(",") == true{       coma == true     }     } } } struct ContentView_Previews: PreviewProvider {   static var previews: some View {     ContentView()   } }

You cannot use if in such a way in SwiftUI.

But you can react to change:

struct ContentView: View {
    @State var dos = ""
    @State var coma : Bool = false
    var body: some View {
        Form {
            TextField("dosis", text: $dos)
                .onChange(of: dos, perform: { _ in
                    if dos.contains(",") {
                        coma = true
                    }
                })
//            if dos.contains(",") == true {
//                coma == true
//            }
        }
    }
}

Or apply modifier to Form:

struct ContentView: View {
    @State var dos = ""
    @State var coma : Bool = false
    var body: some View {
        Form {
            TextField("dosis", text: $dos)
        }
        .onChange(of: dos, perform: { _ in
            if dos.contains(",") {
                coma = true
                print("Changed")
            }
        })
    }
}

Note: when you paste code, use code Paste and Match Style and use Code formatter tool (<>). Much easier to read.

assign value to a bool if contains statement is true
 
 
Q