0

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

1 Answer 1

3

You can't have it be a computed property because, as you saw, it wouldn't be consistent. What you want is a constant (i.e., let), but as you saw, you can't have a constant computed property (if you think about it, it doesn't really make sense for something to both "be the same every time" [constant] and "be recomputed every time" [computed property]). Instead, you want to be doing basically what you did in your first code listing:

let names = ["Zoey", "Chloe", "Amani", "Amaia"]
let randomName = names.randomElement()!

You said that this didn't work because randomName wasn't initialized. I assume you were trying it in this context:

struct MyView: View {
    let names = ["Zoey", "Chloe", "Amani", "Amaia"]
    let randomName = names.randomElement()!

    var body: some View {
        MyOtherView()
    }
}

and it wasn't working. To fix that problem, simply define an initializer like so: (you can add any other parameters that you need to the initializer as well)

struct MyView: View {
    let names = ["Zoey", "Chloe", "Amani", "Amaia"]
    let randomName: String

    init() {
        self.randomName = names.randomElement()!
    }

    var body: some View {
        MyOtherView()
    }
}

note: if you have any @Binding variables that you were passing to this view, you may be a little confused on how to include them in your initializer, so here's a brief example:

struct SuperView: View {
    @State var text: String = ""

    var body: some View {
        SubView(text: self.$text)
    }
}

struct SubView: View {
    @Binding var text: String
    let names = ["Zoey", "Chloe", "Amani", "Amaia"]
    let randomName: String

    init(text: Binding<String>) {
        self._text = text // this accesses the raw Binding<String> variable, allowing you to use the passed value
        self.randomName = names.randomElement()!
    }

    var body: some View {
        SomeOtherView()
    }
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.