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.