CoreData和FetchRequest,ForEach和“无法调用初始化程序”问题

时间:2019-08-27 10:48:56

标签: swiftui ios13 xcode11

我刚刚迁移到使用CoreDate而不是简单的集合。我正在使用iOS13 beta 8和Xcode11 beta 6。

struct BeaconList: View {
    @Environment(\.managedObjectContext) var managedObjectContext
    @FetchRequest(fetchRequest: fetchRequest(), animation: nil) var beacons: FetchedResults<Beacon>

    static func fetchRequest() -> NSFetchRequest<Beacon> {
        let request: NSFetchRequest<Beacon> = Beacon.fetchRequest()
        request.sortDescriptors = [NSSortDescriptor(key: "id", ascending: true)]

        return request
    }

    func buildView(name: String, beacons: [Beacon]) -> AnyView {
        return AnyView (
            Section(header: Text(name)) {
                ForEach(beacons) { beacon in
                    BeaconListEntry(beacon: beacon)
                }
            }
        )
    }

var body: some View {
    TabView {
        NavigationView {
            List {
                buildView(name: "MY BEACONS", beacons: beacons.filter { $0.isActive })
            }
            .navigationBarTitle("Beacons")
            .listStyle(GroupedListStyle())
            .navigationBarItems(trailing: addButton)
        }
        .tabItem {
            Image(systemName: "antenna.radiowaves.left.and.right")
            Text("Beacons")
        }

    }
}

并使用BeaconListEntry如下:

struct BeaconListEntry : View {
   @Binding var beacon: Beacon

    var body: some View {
        HStack {
            Text(verbatim: beacon.name!)
        }
    }
}

(请忽略强制展开,仅用于测试目的)

当我在重写之前将其用于收藏集时,它可以工作,但是现在我收到了消息

Cannot invoke initializer for type 'ForEach<_, _, _>' with an argument list of type '([Beacon], @escaping (Binding<Beacon>) -> BeaconListEntry)'

1. Overloads for 'ForEach<_, _, _>' exist with these partially matching parameter lists: (Data, content: @escaping (Data.Element) -> Content), (Range<Int>, content: @escaping (Int) -> Content)

对哪里看有什么想法?这是使用FetchedResults的正确方法吗?

1 个答案:

答案 0 :(得分:0)

BeaconListEntry初始化程序需要一个类型为Binding的参数,因此您应按以下方式修改调用:

BeaconListEntry(beacon: .constant(beacon))