Swift SwiftUI-滚动视图错误?错误:编译器无法在合理时间内对此表达式进行类型检查;试着分手

Swift SwiftUI-滚动视图错误?错误:编译器无法在合理时间内对此表达式进行类型检查;试着分手,swift,swiftui,Swift,Swiftui,我在这里真的迷路了。这是我脑子里的bug还是XCode bug?这是一个ContentView,其中包含一个列表,用户可以按下按钮跳转到带有相应字母的名称。我有两个问题,一个是上面的错误,第二个是,如果ClientData更改,列表顶部的索引按钮应该得到更新。起初,我想在.onappear中这样做,但是,就像现在一样,我只得到屏幕上可见的名字的第一个字母。因此,我喜欢在对客户端数组排序以收集第一个字母时构建列表 import SwiftUI import PlaygroundSupport im

我在这里真的迷路了。这是我脑子里的bug还是XCode bug?这是一个ContentView,其中包含一个列表,用户可以按下按钮跳转到带有相应字母的名称。我有两个问题,一个是上面的错误,第二个是,如果ClientData更改,列表顶部的索引按钮应该得到更新。起初,我想在.onappear中这样做,但是,就像现在一样,我只得到屏幕上可见的名字的第一个字母。因此,我喜欢在对客户端数组排序以收集第一个字母时构建列表

import SwiftUI
import PlaygroundSupport
import Combine

struct ClientModel : Identifiable, Hashable {
    var id : Int
    var firstName : String
    var lastName : String
}

class ClientData : ObservableObject   {
 @Published var clients : [ClientModel] = [
    ClientModel(id: 1, firstName: "jan", lastName: "Pieters"),
    ClientModel(id: 2, firstName: "kees", lastName: "Jansen"),
    ClientModel(id: 3, firstName: "jaap", lastName: "Jansen"),
    ClientModel(id: 66, firstName: "koos", lastName: "Werkeloos")
 ]
}


struct ContentView: View {
 @ObservedObject var clients = ClientData()
 @State private var indexDict : Dictionary<String, Int> = [:]


 var body: some View {
    ScrollViewReader { scrollProxy in
        VStack {
            HStack {
                ForEach ( indexDict.sorted(by: { (lhs, rhs) -> Bool in
                                            lhs.key < rhs.key}), id: \.key ) { key, value in
                    Button {
                        withAnimation {
                            scrollProxy.scrollTo(value, anchor: .top)
                        }
                    }.buttonStyle(SectionIndexButtonStyle())
                }
            }
            ScrollView {
                LazyVStack {
                    ForEach( clients.clients.sorted(by: { (lhs, rhs) -> Bool in
                                                        lhs.lastName! < rhs.lastName!}), id: \.id ) { client in
                        buildIndex(proxy: scrollProxy, item: client)
                        RowView(text: (client.lastName! + ", " + client.firstName! + ", " + "\(client.id)" ) as String )
                            .onAppear( print(client.lastName!) )
                            .id( client.id )
                    }
                }
            }
        }
    }
    .navigationTitle("Clients")
 }

 func buildIndex(proxy: ScrollViewProxy, item: ClientModel) {
    let firstLetter = item.lastName.trimmingCharacters(in: .whitespaces).prefix(1).uppercased()
    print("firstLetter : \(firstLetter)")

    if indexDict.keys.contains(firstLetter) {
        if indexDict[firstLetter] != nil {
            indexDict[firstLetter] = item.id
            print("did contain the letter but empty id")
        } else {
            print(indexDict[firstLetter] ?? "??")
            print("does contain the letter and id")
        }
    } else {
        print("Not found")
        indexDict[firstLetter] = item.id
        print("does contain now the letter and id")
    }
 }
}

struct MySubView: View {
 @Binding var contentViewConfig : ContentViewConfig
 var body : some View {
    // ...
    Text("press me")

 }
}

struct RowView: View {
 let text: String

 var body: some View{
    Text(text)
        .padding()
        .frame(maxWidth: .infinity, alignment: .leading )
 }
}

struct SectionIndexButtonStyle: ButtonStyle {
 func makeBody(configuration: Self.Configuration) -> some View {
    configuration.label
        .foregroundColor(configuration.isPressed ? Color.gray.opacity(0.2) : Color.accentColor.opacity(0.1))
        .background(configuration.isPressed ? Color.white : Color.gray.opacity(0.1))
        .cornerRadius(15.0)
        .overlay(RoundedRectangle(cornerRadius: 15.0) .stroke(lineWidth: 1.0).foregroundColor(.accentColor))
        .padding(0)
 }
}

PlaygroundPage.current.liveView = UIHostingController(rootView:
                                                    ContentView())
导入快捷界面
导入PlaygroundSupport
进口联合收割机
结构ClientModel:可识别、可哈希{
变量id:Int
var firstName:String
var lastName:String
}
类ClientData:ObservableObject{
@已发布的var客户端:[ClientModel]=[
ClientModel(id:1,姓氏:“jan”,姓氏:“Pieters”),
客户模型(id:2,名:“kees”,姓:“Jansen”),
客户模型(id:3,名字:“jaap”,姓氏:“Jansen”),
客户模型(id:66,名字:“koos”,姓氏:“Werkeloos”)
]
}
结构ContentView:View{
@ObservedObject var clients=ClientData()
@国家私有变量索引信息:字典=[:]
var body:一些观点{
ScrollViewReader{scrollProxy in
VStack{
HStack{
ForEach(indexDict.sorted(by:{(lhs,rhs)->Bool in
lhs.keyBool in
lhs.lastName!一些视图{
配置标签
.foregroundColor(配置.isPressed?颜色.灰色.不透明度(0.2):颜色.重音颜色.不透明度(0.1))
.背景(配置.isPressed?Color.white:彩色.灰色.不透明度(0.1))
.转弯半径(15.0)
.overlay(圆角矩形(拐角半径:15.0)。stroke(线宽:1.0)。foregroundColor(.accentColor))
.填充(0)
}
}
PlaygroundPage.current.liveView=UIHostingController(根视图:
ContentView())

我确实注释了几个部分来检查哪里可以找到crullpit,但仍然得到相同的错误。你知道我如何理解这个问题吗?

你可以从把代码分成几个部分开始——部分/变量/扩展。这将有助于你缩小问题的范围。@pawello我做了,我甚至做了注释。@Asperi,大约1。)以前我在外面用过很多次,它确实有效。我还看到了ScrollViewReader位于Scrollview之外的示例。喜欢-但我会查一查。@Asperi,大约2。)小费不错,我不知道。但是,如果我把它注释掉,它仍然会给我同样的错误。但我注意到,当我把这件事评论出来时,它说这句话仍然是错误的。我应该如何在那里使用indexDict?@LordAnubis ForEach循环在RandomAccessCollection上迭代。您需要决定要做什么:迭代键、值或展平dict。