Swift UI Simulator Crash on Xcode

Hi everyone,

I'm building my first project on Xcode using SwiftUI. While working on a screen that lets people enter the names of players. Additionally, it should and open a popup sheet that lets them delete certain players. While doing this I encountered a strange error. The Preview seems to crash every time I try to delete an entry in the list.

Does anyone have an idea how I could get the following code to work?

Thanks very much, any help is highly appreciated.


Code Block SwiftUI
import SwiftUI
struct Test_17_05_1: View {
   
  @State var pName: [String] = [""]
  @State var playerEnterCount: Int = 0
  @State var showPlayerlist: Bool = false
   
  func delete(at offsets: IndexSet) {
          pName.remove(atOffsets: offsets)
  }
   
  var body: some View {
    
    NavigationView {
       
    VStack {
      Spacer()
      
      HStack {
       
        Image (systemName: "person.fill")
          .foregroundColor(.secondary)
        Text("What's the name of player " +
String(playerEnterCount+1) + "?")
        Spacer ()
      }
       
      HStack {
         
        Image (systemName: "person.fill.badge.plus")
          .foregroundColor(.secondary)
          
      
      }
     
      Spacer()
       
      Button("next player") {
         
        playerEnterCount += 1
         
        pName.append("")
         
      }
      .foregroundColor(.secondary)
      
            
      Spacer()
    }
     
    .navigationBarItems(trailing:
                Button(action: {
                  showPlayerlist.toggle()
                    },
                    label: {
                    Image(systemName: "person.3.fill")
                    Text("Players")
                    }).foregroundColor(.secondary)
    )
    .sheet(isPresented: $showPlayerlist, content: {
       
        List {ForEach(pName, id: \.self) {pName in
          Text(pName)
        }.onDelete(perform: delete)
        }
         
    })
       
    }
     
  }
   
}




I don't understand how your code is supposed to work. How do you enter players ?
And you just append empty names…
And why do you initialise with an empty string and not just empty array ?
  @State var pName: [String] = [] // [""]

So I changed for:
Code Block
Button("next player") {
playerEnterCount += 1
pName.append("\(playerEnterCount)")
}


I deleted names in the sheet, without crash (I could do it with blank names as well, and no crash either).

I tested both in simulator and in Preview. Same result.

So please explain your use case scenario.
Thank you Claude for your help

There should have been the following code in line 35 which somehow I must have accidentally deleted in this post.

Code Block
TextField("...", text: $pName[playerEnterCount])

This would've been the text field that allowed players to change the empty entries to their actual names. This is also what makes the simulator crash. So I know that this is the problem that I don't know an answer to yet...

All this is part of the first view of an app that lets people enter their names to play a "truth or dare" game in the next view.


Accepted Answer
When you perform ondelete, you need to decrement playerEnterCount.
Code Block
func delete(at offsets: IndexSet) {
pName.remove(atOffsets: offsets)
playerEnterCount -= 1
}

At the end of the List, you have an empty name.
So, you could filter the list :
Code Block
List {
ForEach(pName.filter() {$0 != "" }, id: \.self) { aName in
Text(aName)
}.onDelete(perform: delete)
}


Thanks, this works brilliantly!



Swift UI Simulator Crash on Xcode
 
 
Q