Welcome to the forum.
Please format code when you post, with code formatter button.
struct ContentView: View {
var body: some View {
VStack {
var affy = ["hello", "world","babie", "adult"]
var RandomAffy = affy.randomElement()!
Text(affy[1])
Button(action: { affy.randomElement()!
},
label:{Image("dealbutton copy")})
}
The code contains a lot of errors. You should first learn a SwiftUI tutorial to get the basics.
This code would compile:
struct ContentView: View {
// var and constants Should be declared here if you want it to be visible everywhere in View
@State var randomAffy = "" // affy.randomElement()! // State to chage the Text when value changes ; Should start with lowercase
let affy = ["hello", "world","babie", "adult"]
var body: some View {
VStack(alignment: .center) {
Spacer()
Text(affy[1])
Spacer()
Button(action: {
randomAffy = affy.randomElement()!
})
{
Text("My button")
.frame(alignment: .center)
}
Spacer()
Text("Random: \(randomAffy)")
Spacer()
}
.onAppear()
{ randomAffy = affy.randomElement()! }
}
}
But it is unclear what you want to do. Please detail what you mean in the post title.