我有这样的代码,它有 @FetchRequest 加载联系人然后我计算了基于 @State 变量进行过滤的属性
@FetchRequest var fetchRequest: FetchedResults<Contact>
private var contacts : Array<Contact> {
Array(fetchRequest).filter { contact in
if self.sectionSelection == 1 {
return contact.type == "person"
} else if self.sectionSelection == 2 {
return contact.type == "company"
} else {
return true
}
}
}
@State private var sectionSelection : Int = 0
但是列表崩溃,索引超出范围错误!
List {
ForEach(0..<contacts.count) { i in
ZStack {
NavigationLink(destination: ContactDetails(contact: contacts[i])) {