在SwiftData中更改排序

huangapple go评论73阅读模式
英文:

Changing Sort in SwiftData

问题

我正在将一个基本的待办事项列表应用转换为SwiftData。之前,我使用了EditButton(),允许用户移动和重新排序待办事项列表中的元素,但使用SwiftData时,允许用户根据各种选项对列表进行排序可能更合理,例如:

  • 按输入顺序
  • 按字母顺序
  • 按到期日期
  • 已完成

如果我像这样使用@Query创建我的待办事项数组:

@Query var toDos: [ToDoItem]

是否可以添加和更改查询的排序参数?我在底部添加了一个分段控制器,其中包含各种选择的枚举,但我不清楚如何更新现有查询项目的排序参数。同样,添加或删除用于筛选的谓词将非常有用,例如,仅显示已完成或未完成的值。
谢谢!

英文:

I'm converting a basic ToDo list app to SwiftData. I had previously used the EditButton() and allowed elements in the ToDo list to be moved to be reordered, but with SwiftData it'd likely make more sense to allow the user to sort a list based on various options, e.g.:

  • As Entered

  • Alphabetical

  • Due Date

  • Completed
    If I create an array of my todo items using @Query like this:

    @Query var toDos: [ToDoItem]

is it possible to add and change a sort parameter for the Query? I've added a Segmented Control on the bottom with enums for the various choices, but am unclear how I'd update the sort parameter on an existing Query item. Similarly, it would be nice to add or remove a predicate to filter on, say, showing only completed or not completed values.
Thanks!

答案1

得分: 2

我是一名初学者,但我只能告诉你,你可以像这样排序一个查询:

@Query(sort: \ToDoItem.'theVariableYouWantToUseToSort') private var todos: [ToDoItem]

只需将 'theVariableYouWantToUseToSort' 替换为你想要用来排序待办事项的变量即可。

希望能对你有所帮助。祝你有个愉快的一天。

英文:

I am a beginner but all I can tell you is that you can sort a query like that:

@Query(sort: \ToDoItem.'theVariableYouWantToUseToSort') private var todos: [ToDoItem]

Just replace 'theVariableYouWantToUseToSort' by the variable that you want to use to sort your todos.

I hope it helps you. Have a good day.

答案2

得分: 0

将您想要用于设置查询并在视图的初始化程序中重新创建查询的值发送出去。以下是我为我的书准备的示例。在实现了这个初始化程序之后,视图接收到一个字符串,并创建了一个新的查询来搜索该字符串。

@Query var listBooks: [Book]

init(search: String) {
    var predicate = #Predicate<Book> { _ in true }
    if !search.isEmpty {
        predicate = #Predicate<Book> { book in
            book.title.contains(search)
        }
    }
    _listBooks = Query<Book, [Book]>(filter: predicate, sort: \.title, order: .forward)
}
英文:

Send the value you want to use to set the query and recreate the query in the view's initializer. The following is an example I'm preparing for my book. After this initializer is implemented, the view receives a string and a new query is created to search for that string.

   @Query var listBooks: [Book]
   
   init(search: String) {
      var predicate = #Predicate&lt;Book&gt; { _ in true }
      if !search.isEmpty {
         predicate = #Predicate&lt;Book&gt; { book in
            book.title.contains(search)
         }
      }
      _listBooks = Query&lt;Book, [Book]&gt;(filter: predicate, sort: \.title, order: .forward)
   }

huangapple
  • 本文由 发表于 2023年6月12日 09:07:22
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453130.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定