【问题标题】:SwiftUI use relationship predicate with struct parameter in FetchRequestSwiftUI 在 FetchRequest 中使用带有结构参数的关系谓词
【发布时间】:2019-11-09 21:34:15
【问题描述】:

如何将@FetchRequest 与传递给struct 的参数一起使用?

struct TodoItemView: View {
    var todoList: TodoList
    @FetchRequest(
        entity: TodoItem.entity(),
        sortDescriptors: [NSSortDescriptor(key: "order", ascending: true)],
        predicate: NSPredicate(format: "todoList == %@", todoList)
    ) var todoItems: FetchedResults<TodoItem>
    ...

我通过TodoListtodoList 设置为TodoItemOne 关系。

它给了我错误:

不能在属性初始化器中使用实例成员“todoList”;属性初始化器在“self”可用之前运行

如果我不能在初始化程序中使用@FetchRequest,我该如何处理这种关系?另外,我应该在这里的某个地方使用todoList.objectID 吗?

【问题讨论】:

    标签: ios swift core-data swiftui


    【解决方案1】:

    想通了:

    var todoList: TodoList
    @FetchRequest var todoItems: FetchedResults<TodoItem>
    
    init(todoList: TodoList) {
        self.todoList = todoList
        self._todoItems = FetchRequest(
            entity: TodoItem.entity(),
            sortDescriptors: [NSSortDescriptor(key: "order", ascending: true)],
            predicate: NSPredicate(format: "todoList == %@", todoList)
        )
    }
    

    感谢类似的回答:SwiftUI View and @FetchRequest predicate with variable that can change

    【讨论】:

      猜你喜欢
      • 2020-01-12
      • 2017-05-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-07
      • 1970-01-01
      相关资源
      最近更新 更多