SwiftUI ForEach“;无法推断复杂的闭包返回类型;添加显式类型以消除歧义;

SwiftUI ForEach“;无法推断复杂的闭包返回类型;添加显式类型以消除歧义;,swift,xcode,swiftui,Swift,Xcode,Swiftui,我已经在这上面呆了好几天了,现在想弄清楚问题是什么 我建立了一个视图,其中列出了我可以使用核心数据输入和保存的数据 我的数据模型看起来是这样的 我可以将数据添加到列表中,然后毫无疑问地将其全部读回,但是当我尝试过滤提取的结果时,我会得到标题中的错误。错误大约是代码的一半。 import CoreData struct CollectionRow: View { var collectionDate = Date() let dateFormatter = DateForma

我已经在这上面呆了好几天了,现在想弄清楚问题是什么

我建立了一个视图,其中列出了我可以使用核心数据输入和保存的数据

我的数据模型看起来是这样的

我可以将数据添加到列表中,然后毫无疑问地将其全部读回,但是当我尝试过滤提取的结果时,我会得到标题中的错误。错误大约是代码的一半。
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)))
        }
    }
}

SwiftUI中的
if/else
只能在
@ViewBuilder
闭包中使用,因此
ForEach
失败

更多关于函数生成器的信息

一个简单的解决方案是像这样包装代码:

struct-ForEachBuilder:View其中Content:View{
私人出租内容:内容
init(@ViewBuilder内容:()->content){
self.content=content()
}
var body:一些观点{
内容
}
}
您的
ForEach
将成为:

ForEach(集合,id:\.self){中的集合
创造者{
如果self.hospital.id==collection.hospital{
//您的导航链接
}
}
}

SwiftUI中可能重复的错误消息往往有点随机。我的建议是注释掉代码中的一些部分,直到得到一个有意义的错误。我的直觉是,您应该使集合符合可识别性,然后在第二次查看时声明ForEach(集合)。这是整个ForEach的错误。SwiftUI中的ForEach不是您从集合中了解的ForEach。在SwiftUI中,它用于构造列表,而我认为您正在使用它过滤掉一个元素。我说得对吗?您是否试图仅为具有匹配id的医院显示一个链接?不是单个链接,仅显示匹配的链接。官方教程在ForEach中使用if语句来过滤结果如果if/else语句只能在@ViewBuilder语句中使用,那么官方教程怎么能像我一样使用if语句呢?@Shuttleu你能发布一个到教程的链接吗?第5节,步骤3相同的问题。在本教程中,可能未使用CoreData。但事实上这不应该成为理由。
ForEach(landmarkData) { landmark in
                    if !self.showFavoritesOnly || landmark.isFavorite {
                        NavigationLink(destination: LandmarkDetail(landmark: landmark)) {
                            LandmarkRow(landmark: landmark)
                        }
                    }
                }