【发布时间】:2019-09-20 20:33:32
【问题描述】:
我已经被困在这个问题上好几天了,现在试图找出问题所在。
我已经构建了一个视图,其中列出了我可以使用核心数据放入和保存的数据。
我可以将数据添加到列表中并将其全部读回而没有任何问题,但是当我尝试过滤获取的结果时,我得到了标题中的错误。错误大约是代码的一半。import CoreData
struct CollectionRow: View {
var collectionDate = Date()
let dateFormatter = DateFormatter()
var body: some View {
dateFormatter.dateStyle = .short
dateFormatter.locale = Locale(identifier: "en_GB")
return Text(dateFormatter.string(from: collectionDate))
}
}
struct CollectionList: View {
var hospital = Hospital()
@Environment(\.managedObjectContext) var managedObjectContext
@FetchRequest(
entity: Collection.entity(),
sortDescriptors: [
NSSortDescriptor(keyPath: \Collection.date, ascending: true)
]
) var collections: FetchedResults<Collection>
@State private var collectionName = ""
var body: some View {
NavigationView {
List {
VStack {
ForEach(collections, id: \.self) { collection in //Unable to infer complex closure return type; add explicit type to disambiguate
if self.hospital.id == collection.hospital {
NavigationLink(destination: CollectionRow(collectionDate: Date())){
CollectionRow(collectionDate: Date())
}
}
}
TextField("Test", text: $collectionName)
Button(action: {
let col = Collection(context: self.managedObjectContext)
col.date = Date()
col.id = Int16(self.collections.endIndex + 1)
col.hospital = self.hospital.id
self.hospital.addToHospToCol(col)
do {
try self.managedObjectContext.save()
} catch {
print(error)
}
self.collectionName = ""
}) {
Text("New Collection")
}
}
}
.navigationBarTitle(Text((hospital.name ?? "") + " - " + String(hospital.id)))
}
}
}
编辑:官方教程在 ForEach 中使用了 if 语句,那我为什么不能呢?
ForEach(landmarkData) { landmark in
if !self.showFavoritesOnly || landmark.isFavorite {
NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
LandmarkRow(landmark: landmark)
}
}
}
【问题讨论】:
-
SwiftUI 中的错误消息往往有点随机。我的建议是注释掉你的代码,直到你得到一个有意义的错误。我的直觉是你应该让 Collection 符合 Identifiable 然后声明 ForEach(collections)
-
再看一遍,错的是整个 ForEach。 SwiftUI 中的 ForEach 不是你从集合中知道的 forEach。在 SwiftUI 中,它用于构造列表,而我认为您正在使用它来过滤掉一个元素。我对吗?您是否尝试仅针对具有匹配 ID 的医院显示一个链接?
-
不是一个,只有匹配的。官方教程在 ForEach 中使用 if 语句来过滤结果