辅助选择器的“ForEach”给了我致命错误:索引超出范围

Posted

技术标签:

【中文标题】辅助选择器的“ForEach”给了我致命错误:索引超出范围【英文标题】:Secondary Picker's 'ForEach' gives me Fatal Error: Index Out of Range 【发布时间】:2020-10-03 17:56:27 【问题描述】:

我每次启动 secondary 选择器时都会收到此错误。 第一个选择器工作正常。 但是,当 我切换选择器 并滚动时,我得到以下信息:

这是我的整个代码(作为这个问题的测试而编写):

import SwiftUI

struct ContentView: View 
    @State private var selectedItem = 0
    @State private var isMainPickerHidden = false
    @State private var isSecondaryPickerHidden = true

    var colors = ["Red", "Green", "Blue", "Tartan"]
    var sizes = ["Tiny", "Small", "Medium", "Large", "Super Size"]

    var body: some View 
        ZStack 
            Color.yellow.edgesIgnoringSafeArea(.all)
            ZStack 
                VStack 
                    Picker(selection: $selectedItem, label: Text("Please choose a color")) 
                        ForEach(colors.indices, id: \.self) 
                            Text(self.colors[$0])
                        
                    .hiddenConditionally(isHidden: isMainPickerHidden)

                    Text("You selected: \(colors[selectedItem])")
                        .hiddenConditionally(isHidden: isMainPickerHidden)
                

                VStack 
                    Picker(selection: $selectedItem, label: Text("Please choose a size")) 
                        ForEach(sizes.indices, id: \.self) 
                            Text(self.sizes[$0])
                        
                    .hiddenConditionally(isHidden: isSecondaryPickerHidden)

                    Text("You selected: \(sizes[selectedItem])")
                        .hiddenConditionally(isHidden: isSecondaryPickerHidden)
                    Spacer()
                    Button(action: 
                        isSecondaryPickerHidden = !isSecondaryPickerHidden
                        isMainPickerHidden = !isMainPickerHidden
                    ) 
                        Text("Switch Pickers")
                    .padding()
                
            
        
    


// =========================================================================================================

extension View 
    func hiddenConditionally(isHidden: Bool) -> some View 
        isHidden ? AnyView(hidden()) : AnyView(self)
    


struct ContentView_Previews: PreviewProvider 
    static var previews: some View 
        ContentView()
    

ForEach 避免此问题的正确语法是什么?

【问题讨论】:

【参考方案1】:

这是因为您为两个选择器使用了相同的 selectedItem

如果在第二个选择器中选择了最后一项(索引 4),然后切换到第一个选择器(最大索引 = 3),则在此行中:

Text("You selected: \(colors[selectedItem])")

您将尝试访问 超出范围的索引。


要解决此问题,您可以为每个选择器使用单独的 @State 变量:

struct ContentView: View 
    @State private var selectedColorIndex = 0
    @State private var selectedSizeIndex = 0
Picker(selection: $selectedColorIndex, label: Text("Please choose a color")) 
Picker(selection: $selectedSizeIndex, label: Text("Please choose a size")) 

【讨论】:

以上是关于辅助选择器的“ForEach”给了我致命错误:索引超出范围的主要内容,如果未能解决你的问题,请参考以下文章

我正在尝试附加 alamofire 循环并放入 sqlite db,但它给了我错误

我想在useEffect react Js中使用带有foreach的axios,但它给了我(无法读取属性Symbol(Symbol.iterator)错误?

索引超出范围 SwiftUI

SwiftUI .onDelete 抛出致命错误:索引超出范围

SwiftUI - 致命错误:从数组中删除元素时索引超出范围

KIvy GL 致命错误