I cant declare a variable inside swift ui view block
var body: some View {
let isHuman = false
Text("Levels \(isHuman)")
}
you shouldn't create a variable inside the SwiftUI builder block, you should create it out of the body scope,
var isPlaying = false
var body: some View {
Text("Levels \(isPlaying)")
}
Swift UI uses a function builders block that can only contain content that is understandable by the builder. Then what you should write inside the builder block is only View Type and [View] However, if you want to declare a variable you could work around it by introducing it to a new subclass
The purpose of this feature is to enable the creation of embedded DSLs in Swift -- allowing you to define content that gets translated to something else down the line
Also, you could use.
@State var isPlaying: Bool = false
Note
review your code twice before doing the workaround, probably you have a misunderstanding about how swift-UI works?
Not so bad, you can ... another question whether you really need it, but when you really need, you do know that you can. You just need to remember that view builder must return some View and it must be same type in all possible branches
var body: some View {
let value = "" // construct somewhere or something
return Text("Levels \(value)")
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With