SwiftUI绑定到计算属性? [英] SwiftUI Binding to Computed Property?

查看:144
本文介绍了SwiftUI绑定到计算属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个类,一个是ContentView,它显示我的数据源中的信息。数据源是我的CharacterRepository。

I have two classes, one is the ContentView who is displaying the information from my data source. The data source is my CharacterRepository.

我现在正在努力确保我的CharacterRepository内始终有一个排序列表。

What I'm struggling with right now is making sure I always have a sorted list inside of my CharacterRepository.

这是我到目前为止的代码:

Here's the code I have so far:

class CharacterRepository: ObservableObject {
    @Published public var characters = [Character(name: "Nott the Brave",
                                                  initiative: 23,
                                                  isActive: false),
                                        Character(name: "Caduceus Clay",
                                                  initiative: 2,
                                                  isActive: false),
                                        ...]
    ...
}

struct InitiativeTrackerScreen: View {
    @EnvironmentObject var characterRepository: CharacterRepository

    var body: some View {
        NavigationView {
            VStack {
                List {
                    ForEach(characterRepository.characters) { entry in
                        CharacterListElement(character: entry)
                    }
    ...

现在,我打算使用的方法是将字符转换为变量放入每次执行get时都会运行 sorted by功能的计算属性。不幸的是,在SwiftUI中尚无法绑定到计算属性(不确定,是否曾经如此?)。

Now my intended approach would be something like turning the characters variable into a computed property that runs the "sorted by" function every time the get gets executed. Unfortunately, Binding to a computed property is not yet possible in SwiftUI (not sure, will it ever be?).

有人可以帮我这个忙吗?我不想回到每次更改时重新排序和重绘的旧方法。这不是为什么我将SwiftUI与ist甜绑定一起使用。

Can someone please help me with this? I don't want to go back to the old approach of sorting and redrawing every time something changes. That's not why I am using SwiftUI with ist sweet bindings.

推荐答案

我认为对数据进行排序可能不必要的开销

I think it may be unnecessary overhead to be sorting the data in the property getter.

简单(和注重性能的)解决方案是在数据更改时对其进行排序,然后更新 @Published 非计算属性并绑定到该属性:

The simple (and performance-conscious) solution is to sort the data when it changes, and then to update a @Published non-computed property and bind to that:

class CharacterRepository: ObservableObject {
    func updateCharacters(_ characters: [Character]) {
       self.characters = characters.sorted() // or whatever sorting strategy you need...
    }

    @Published public var characters = [Character(name: "Nott the Brave",
                                                  initiative: 23,
                                                  isActive: false),
                                        Character(name: "Caduceus Clay",
                                                  initiative: 2,
                                                  isActive: false),
                                        ...]
    ...
}

如果您希望以牺牲性能为代价而做到这一点,则可以手动创建 绑定 -类似于这样:

If you prefer to be über-purist about this at the cost of performance, then you can manually create a Binding - something like this:

class CharacterRepository: ObservableObject {
   let sortedCharacters: Binding<[Character]>
   ...


   init() { 
      self.sortedCharacters = .init(get: { return characters.sorted() }, set: { self.characters = $0 })
   }

   ...
}

这篇关于SwiftUI绑定到计算属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆