简体   繁体   中英

SwiftUI - Removing Item from Array causes Fatal Error: Index out of Range

I am new to SwiftUI and I am trying to build an Image-Gallery, where you are able to remove an image:

My code is working partly, when there are more elements the only element I can't remove is the last one.

Something which is weird: When I have only one element I can remove it in one View and in the other it causes the Index out of Range error

This is my code

struct ImageSlider: View {
@Binding var images: [DefectImage]
@Binding var imagesTitels: [String]
@Binding var edit: Bool

var body: some View {
    ScrollView(.horizontal) {
        HStack {
            if self.images.count > 0 {
                ForEach(self.images) { img in
                    VStack {
                        if !self.edit {
                            DefImage(url: "", image: img.image)
                            Text(self.imagesTitels[self.getIdOfImg(img: img)])
                                .frame(width: 135)
                        }
                        else {
                            Button(action: {
                                self.imagesTitels.remove(at: self.getIdOfImg(img: img))
                                self.images.remove(at: self.getIdOfImg(img: img))
                            }){
                                DefEditImage(url: "", image: img.image)
                            }
                            .buttonStyle(PlainButtonStyle())

                            VStack {
                                TextField("", text: self.$imagesTitels[self.getIdOfImg(img: img)])
                                    .frame(width: 135)
                                    .offset(y: 6)
                                Rectangle()
                                    .frame(width: 135, height: 1.0, alignment: .bottom)
                                    .foregroundColor(Color.gray)
                            }
                        }
                    }
                }
            }
        }
    }
}



func getIdOfImg(img: DefectImage) -> Int {
    var countId: Int = 0
    for item in self.images {
        if img.id == item.id {
            return countId
        }

        countId += 1
    }

    return -1
}

The Error:

Fatal error: Index out of range: file /Library/Caches/com.apple.xbs/Sources/swiftlang/swiftlang-1103.2.25.8/swift/stdlib/public/core/ContiguousArrayBuffer.swift, line 444

The error is not in one of the arrays: Screenshot of Error

Why dodn't you try changing your function getIdOfImg. not using a for loop instead using

func getIdOfImg(img: String) -> Int {
    if let index = self.images.firstIndex(of: img) {
        return index
    } else {
        return -1
    }
}

and also rewriting the block

Button(action: {
    self.imagesTitels.remove(at: self.getIdOfImg(img: img))
    self.images.remove(at: self.getIdOfImg(img: img))
})

with

Button(action: {
    let index = self.getIdOfImg(img: img)
    if index > 0 && index < self.images.count {
        self.imagesTitels.remove(at: index)
        self.images.remove(at: index)
    }
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM