1
votes

thanks for your attention. I'm super suuuuper new in coding.. I think it's a very basic but I can't figure it out,,

I'm trying to make 6 random numbers! like 'powerball', 'lotto', or any kind of random unique numbers required games! I'm trying with swiftUI

I can make a one random number.. like this way

struct ContentView: View {

@State private var showDetails = false


var body: some View {
    VStack {
        Spacer()
        if showDetails {
            
            Text("\(Int.random(in: 1...45))")
                .font(.largeTitle)
                
        }
        Button(action: {
            self.showDetails.toggle()
        }) {
            Text("BUTT")
                .padding(.bottom, 30)
        }
    }
    
    
}

and I can make it 6 times (Foreach) but there are overlap numbers!

And here is what I've done..

import SwiftUI


struct ContentView: View {

@State private var showDetails = false

var randomArray: [Int] = []
var randomNumbers = Int.random(in: 1...45)


var body: some View {
    VStack {
        Spacer()
        if showDetails {
            
            randomArray.append(randomNumbers)
            
            Text("\(randomArray)")
                .font(.largeTitle)
            
        }
        Button(action: {
            self.showDetails.toggle()
            
        }) {
            Text("BUTT")
                .padding(.bottom, 30)
        }
    }
    
    
}
}


struct ContentView_Previews: PreviewProvider {
static var previews: some View {
    ContentView()
}

}

it show me

'Type '()' cannot conform to 'View'; only struct/enum/class types can conform to protocols' 'Cannot use mutating member on immutable value: 'self' is immutable'

I think the 'randomArray' or 'randomNumers' are correct but 'randomArry.append(randomNumbers)' or Text("(randomArray)")' are maybe wrong..

I tried 2 day and I find a lot of things but I can't still make it.. so.. could you give me a little help? Thanks!

2

2 Answers

1
votes

An easy way to get an array of 6 unique random numbers is

let randomNumbers = Array(1...45).shuffled().prefix(6)

I am not entirely sure what you want to do in your UI but here is one solution

struct ContentView: View {
    @State private var showDetails = false
    let randomNumbers = (1...45).shuffled().prefix(6)

    var body: some View {
        VStack {
            Spacer()
            if showDetails {
                ForEach(randomNumbers, id: \.self) { number in
                    Text(String(number))
                }
            }
            Button(action: {
                self.showDetails.toggle()
            }) {
                Text("BUTT")
                    .padding(.bottom, 30)
            }
        }
    }
}

If you want to generate a new set of numbers for each press of the button then you can create a function that generates the numbers

func generateRandomNumbers() -> [Int] {
    Array((1...45).shuffled().prefix(6))
}

and call it in the ForEach

ForEach(generateRandomNumbers(), id: \.self) { number in
0
votes

The code appears to be trying to add a random number to an array as a side-effect of when a button is pressed to display the contents of the random number array.

The error message is unclear, but what is happening is the compiler has spotted a state mutation in the code whilst laying out the UI that could cause a race condition.

To get things working, the code needs to separate out UI layout from state update so that they occur in separate cycles.

Here's one example of how that might be done:

import SwiftUI

struct ContentView: View {
    @State var randomArray: [Int] = []
    var arrayString: String {
        if randomArray.count == 0 {
                return  "No numbers"
        } else {
            return randomArray.map { "\($0)" }.reduce("") { $0 + " " + $1 }
        }
    }

    var body: some View {
        VStack {
            Text("My random numbers = \(arrayString)")
            Button("New random number") {
                randomArray.append(Int.random(in: 1 ... 45))
            }
        }
    }
}

struct ContentView_Previews: PreviewProvider {
    static var previews: some View {
        ContentView()
    }
}

Good luck and have fun.

PS: If learning Swift, the Hacking With Swift website is an excellent reasource