I followed the instructions at https://developer.apple.com/documentation/swift/array/2994747-randomelement that give this example
let names = ["Zoey", "Chloe", "Amani", "Amaia"]
let randomName = names.randomElement()!
// randomName == "Amani"
This won't work as randomName is not initialised so I tried a workaround:
let words = [
"anbieten","anbietend","angeboten"]
var random : String { words.randomElement()!}
to give some context I am using this random variable for a game that should display the same word 3 times so I need to be able to be sure that the value of "random" contains yes a random word from the array but always the same random one.
Unfortunately when I use the variable in SwiftUI
NavigationLink(destination: ResultView(choice: "Arial")) {
Text("\(random)").font(.custom("Arial", size: 60))
}
NavigationLink(destination: ResultView(choice: "San Francisco")) {
Text("\(random)").font(.system(size: 60))
}
it obviously show two different elements of the array.
I did try to change my random as per example in the apple developer site to a let
let random : String { words.randomElement()!}
but this returns
let' declarations cannot be computed properties