dimanche 12 janvier 2020

Swift return random element from array into text

This problem which i know should be very easy has stumped me for hours.

I try to get a random element from a simple array into a text in swiftui but i get errors i dont understand, the recent error i got was: Instance method 'appendInterpolation' requires that '[Result]' conform to '_FormatSpecifiable'

what would be the simplest way to implement this? I understand this has been answered many times but I am too bad at coding to understand when i try to google it.

struct Result: Identifiable {
    var id = UUID()
    var score: Int
}



struct ContentView: View {

    @State private var showDetails = false

      @State var results = [Result(score: 8), Result(score: 5), Result(score: 10)]


        var body: some View {
            VStack {

                Button(action: {

                    self.results.randomElement()
                }) {
                    Text("Button title, \(results)")
                }


                ForEach(results) { result in
                    Text("Result: \(result.score)")


                }
            }
        }
}



Aucun commentaire:

Enregistrer un commentaire