无法推断复杂的闭包返回类型 SwiftUI ForEach
Posted
技术标签:
【中文标题】无法推断复杂的闭包返回类型 SwiftUI ForEach【英文标题】:Unable to infer complex closure return type SwiftUI ForEach 【发布时间】:2020-01-08 12:04:00 【问题描述】:我正在尝试显示过滤后的 List
,其中仅包含与 self.day[index]
完全匹配的 subjects
。但是,当我尝试为此使用if
子句时,我收到错误Unable to infer complex closure return type; add explicit type to disambiguate
。有人可以找到任何其他方法将subject
过滤为subject.day
以等于self.days[index]
吗?这是我的代码,谢谢:
import SwiftUI
import CoreData
struct ProfileView: View
@Environment(\.managedObjectContext) var moc: NSManagedObjectContext
@FetchRequest(
entity: Subject.entity(),
sortDescriptors:[
//NSSortDescriptor(keyPath: \Subject.day, ascending: true),
NSSortDescriptor(keyPath: \Subject.start, ascending: true)
]
) var subjects: FetchedResults<Subject>
@State private var showAddScreen = false
@State private var name: String = ""
@State private var surname: String = ""
let days = ["Pondelok", "Utorok", "Streda", "Štvrtok", "Piatok"]
var body: some View
Form
ForEach(0 ..< days.count) index in
Section
Text(self.days[index])
List
ForEach(self.subjects, id: \.self) subject in //here the shows an error, If I remove the if clause, it works, but obviously I don't have subjects filltered, which is what I need
if subject.day == self.days[index]
SubjectCell(name: "\(subject.name!)",
place: "\(subject.place!)",
start: self.formatTime(date: subject.start ?? Date()),
end: self.formatTime(date: subject.end ?? Date()),
type: subject.type,
occurance: subject.occurance)
.onDelete(perform: self.deleteSubject)
【问题讨论】:
【参考方案1】:将整个if ..
包装成Group ..
解决了问题
【讨论】:
【参考方案2】:根据相同的标准过滤掉主题,然后返回它们的列表不是更好吗?有机会在FetchRequest
或List
内部进行,然后使用return ForEach(filteredSubjects, id: \.self) ...
。
【讨论】:
以上是关于无法推断复杂的闭包返回类型 SwiftUI ForEach的主要内容,如果未能解决你的问题,请参考以下文章
SwiftUI 如何修复“无法推断复杂的闭包返回类型;添加显式类型以消除歧义”
SwiftUI ForEach“无法推断复杂的闭包返回类型;添加显式类型以消除歧义”