Q: SwiftIU, I want to reset my list (remove all list items) when 10 items added to the list struct ContentView: View {
    
    var array = ["Fire", "Water", "Earth", "Air", "Emotion"]
    
    @State var randomList = [String]()
    
    var body: some View {
        
        VStack  {
            
            List (randomList, id: \.self) { ranList in
                Text(ranList)
                }
            Button("Add to list")   {
                let ranIndexNumber = Int.random(in: 0...array.count-1)
                randomList.append(array[ranIndexNumber])
                   
            }

Hi everyone, I am new to the coding and to stack overflow website, I am learning SwiftUI atm and this code above was a "challenge" which is; as I tap the button, the code adds random items from array list above, to the list. I managed to write the code, but I wanted to add another function to the button;

*

*When tapped, in addition to adding items to the list, I want it to remove all items if item count in the list exceed 10. How am I suppose to do that, I don't know/not yet learned a way to count the items in a list to use in an IF statement.

Thanks for the help in advance
P.S. if there is any way to add my code with right colors while posting, I would be grateful if you shortly describe how to do it.Thanks

A: struct ContentView: View {
    
    var array = ["Fire", "Water", "Earth", "Air", "Emotion"]
    
    @State var randomList = [String]()
    
    var body: some View {
        
        VStack  {
            
            List (randomList, id: \.self) { ranList in
                Text(ranList)
                }
            Button("Add to list")   {
                let ranIndexNumber = Int.random(in: 0...array.count-1)
                if randomList.count >= 10 {
                    randomList = [array[ranIndexNumber]]
                } else {

                   randomList.append(array[ranIndexNumber])
                }

                   
            }

