ios Swift/ContiguousArrayBuffer.swift:600:致命错误:索引超出范围

ntjbwcob  于 2023-08-08  发布在  iOS
关注(0)|答案(1)|浏览(190)

我有一个全局数组:

let genderList = ["Not Selected", "Woman", "Man", "Non Binary", "Trans", "Would rather not say"]

字符串
结构示例变量:

@State var selectedGender = genderList[0]


在我的视图中添加一个选择器:

Picker("Please select your gender", selection: $selectedGender) {
    ForEach(genderList, id: \.self) {
        Text($0)
        }
}.pickerStyle(.wheel)


对于我的生活,我似乎无法理解为什么这会引发以下错误:
Swift/ContiguousArrayBuffer.swift:600:致命错误:索引超出范围
我也试过类似的错误:

Picker("", selection: $selectedGender) {
    ForEach(genderList.indices, id: \.self) { index in
        Text(genderList[index])
    }
}.pickerStyle(.wheel)


任何帮助将不胜感激。谢谢你的好意

h9a6wy2h

h9a6wy2h1#

我有同样的错误,我找到了解决方案:
1.第一个月

ZStack {
        Color.black.opacity(0.2)
             .ignoresSafeArea()
       View() // your content here
        if shouldShowPicker {
            assetsPicker(selection: $viewModel.selection,
                   pickerModel: viewModel.pickerModel)
        }
    }.animation(.easeInOut, value: shouldShowPicker)

字符串
拾取器应隐藏动画否则将崩溃。我不知道为什么,但Color.black.opacity(0.2).ignoresSafeArea()使它的动画流畅。

相关问题