0

I'm building this project and trying to keep the code in separate views. This is the array I'm working with:

    struct Nebula: Identifiable{
        var id = UUID()
        var title: String
        var palette: [Palette]
        var show: Bool
    }
    
    struct Palette : Identifiable{
        var id = UUID()
        var color: Color
        var hexValue: String
    }

This is the code I'm working with:-

import SwiftUI

struct NewHome: View {
    @State var palettes = nebulas
    
    var body: some View {
        VStack {
            ForEach(self.palettes.indices, id: \.self) { idx in
                SubView(palette: self.$palettes[idx])
                    .contextMenu {
                        ColorBarContextMenuView(
                            nebs: self.$palettes,
                            index: idx)
                    }
            }
            
        }
    }
}

struct SubView: View {
    @Binding var palette: Nebula
    
    var body: some View {
        HStack(spacing: 0.0) {
            ForEach(self.palette.palette.indices, id: \.self) { index in
                GeometryReader { geometry in
                    Rectangle()
                        .fill(palette.palette[index].color.opacity(0.7))
                    
                }
                .frame(height: 90)
                
            }
        }
        .clipShape(RoundedRectangle(cornerRadius: 10, style: .continuous))
        
    }
}


struct ColorBarContextMenuView: View {
    @Binding var nebs: [Nebula]
    
    var index: Int
    
    var body: some View {
        VStack{
            Button(action: {
                self.nebs.removeAll{$0.id == self.nebs[index].id}

            }, label:
            {
                HStack{
                    Text("Delete")
                    Image(systemName: "trash.fill")
                }
            })
        }
    }
}

The trouble comes when I try to delete from Nebula array and I get the usual:-

Fatal error: Index out of range: file Swift/ContiguousArrayBuffer.swift, line 444

Does anyone know of how I can solve this as I've tried various solutions but nothing seems to be working for me.

  • 1
    There were dozens of posts about *Fatal error: Index out of range on delete* - try to search at first. – Asperi Oct 13 '20 at 15:59
  • Does this answer your question? [Deleting list elements from SwiftUI's List](https://stackoverflow.com/questions/63079221/deleting-list-elements-from-swiftuis-list) – New Dev Oct 13 '20 at 16:03
  • What is on line 444 ? – koen Oct 13 '20 at 16:32
  • @koen That refers to swift framework class ContiguousArrayBuffer, google it and you find quite a lot of index out of range errors with this line number :) – Joakim Danielson Oct 13 '20 at 17:16
  • StackOverflow search about 'Fatal Error: Index out of range' was my first port of call. I couldn't find the right solution which resolved my specific problem. – davidNorris Oct 14 '20 at 09:15

0 Answers0