a a
a a

Reputation: 1

How to run two times in swiftUI

I am new in swiftUI, and I know how to do it in Xcode to make two different random "a" and "b",and print the output. However, I do not know how in SwiftUI to show two different "a" and "b".

It seems like if I use var again, it will deny it.

import SwiftUI

struct ContentView: View {
    var a=Int.random(in:80...90)
    var b=Int.random(in:60...70)
    var body: some View {
        VStack {
            Text ("\(a)+\(b)=")
            Text ("\(a)+\(b)=")

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

Upvotes: 0

Views: 133

Answers (1)

Fredy
Fredy

Reputation: 2063

You could do something like this:

struct ContentView: View {
var body: some View {
    VStack {
        Text ("\(Int.random(in:80...90))+\(Int.random(in:80...90))=")

        Text ("\(Int.random(in:80...90))+\(Int.random(in:80...90))=")


    }
}

} If you use the same var a and var b for both text fields the same numbers will always appear regardless of how many times you run Int.random

Upvotes: 1

Related Questions