Skip to content

Instantly share code, notes, and snippets.

@ucargiray
Last active September 3, 2021 13:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ucargiray/8b7dba0368c3bd475c3658552905fdbc to your computer and use it in GitHub Desktop.
Save ucargiray/8b7dba0368c3bd475c3658552905fdbc to your computer and use it in GitHub Desktop.
Second example for @State property wrapper.
import SwiftUI
struct ContentView: View {
@State private var nameString : String?
var nameArray : [String] = ["Giray","Eren","Feyza","Can","Emre","Mehmet","Buse"]
var body: some View {
VStack {
Text(nameString ?? "No name")
Button("Change text") {
nameString = getRandomElement(from: nameArray)
}
}
}
func getRandomElement(from array: [String]) -> String {
return array.randomElement() ?? "No name"
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment