Last active
September 3, 2021 13:56
-
-
Save ucargiray/8b7dba0368c3bd475c3658552905fdbc to your computer and use it in GitHub Desktop.
Second example for @State property wrapper.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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