简体   繁体   English

SwiftUI - 从数组中删除项目导致致命错误:索引超出范围

[英]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:我是 SwiftUI 的新手,我正在尝试构建一个图像库,您可以在其中删除图像:

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错误不在 arrays 之一中:错误截图

Why dodn't you try changing your function getIdOfImg.为什么不尝试更改您的 function getIdOfImg。 not using a for loop instead using不使用 for 循环,而是使用

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)
    }
})

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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