swiftui - CoreData 和 FetchRequest,ForEach 问题和“无法调用初始化程序”
问题描述
我只是迁移到使用 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
吗?
解决方案
BeaconListEntry
初始化程序需要一个具有 type 的参数,Binding
因此您应该按如下方式修改您的调用:
BeaconListEntry(beacon: .constant(beacon))
推荐阅读
- javascript - 为什么当我将第三个帖子推入数组时,此代码片段仅显示 2 个帖子?
- neo4j - 一个密码查询中的两个全文调用的性能问题
- sql - 将静态 SQL 查询转换为存储过程上的动态 SQL
- reactjs - React Redux Actions 类型安全
- oracle - 显示表格行中的替代列
- python - 如何修复:jinja2.exceptions.UndefinedError: 'csrf_token' 未定义
- python - 使用 selenium 下载 div 元素内的文件 - Python
- c - 十一2。如何忽略修饰键的事件?
- c++ - 当我在OpenGL中按下一个键时无法绘制形状
- azure - 如何小写 Azure DevOps 管道变量名称