SwiftUI中的计算(NSObject)属性不会更新视图 [英] Computed (NSObject) Properties in SwiftUI don't update the view

查看:38
本文介绍了SwiftUI中的计算(NSObject)属性不会更新视图的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因此,我想有一个 Text 可以根据我的CoreData模型的内容更改其内容.为此,我在Xcode beta 4中使用了计算属性,但是它似乎不再起作用.要么是错误,要么是我看不到其他问题?

So, I want to have a Text that changes its content based on the contents of my CoreData Model. To do that I used a computed property in Xcode beta 4 but it doesn't seem to work anymore. Either that's a bug or there is some other issue I don't see?

我确切的问题是,在我的商店中调用 self.objectWillChange.send()时,我的View(以及计算的属性)似乎没有更新.

The problem I have exactly is that my View (and the computed property) don't seem to get updated when self.objectWillChange.send() is called in my store.

我还尝试将var'导出'到商店中并从那里获取它,结果相同...

I also tried to 'export' my var into the store and get it from there, with the same result...

我只是在另一个类上尝试了同样的方法,但它只对 objectWillChange.send()无效,而对 @Published 无效,即使继承了该类也无法正常工作来自NSObject ...

I just tried the same with another class and it didn't work with just objectWillChange.send() but only with @Published however, even that stopped working if the class inherited from NSObject...

我刚刚发现:

struct Today: View {
    @EnvironmentObject var myStore: DateStore
    var hasPlans: Bool {
        guard let plans = myStore.getPlans() else { return false }
        return plans.isEmpty
    }

    var body: some View{
        VStack{
            Text(hasPlans ? "I have plans":"I have time today")
            Button(action: {
                self.myStore.addPlans(for: Date())
            }) {
                Text("I have plans")
            }
    }
}

class DateStore: NSObject, ObservableObject, NSFetchedResultsControllerDelegate {
    private var fetchedResultsController: NSFetchedResultsController<DateStore>
    //...
    public func addPlans(for date: Date){
        //{...}
        if let day = self.dates.first(where: { $0.date == date}){
            day.plans += 1
            saveChanges()
        }else{
            self.create(date: dayDate)
        }
    }

    func controllerDidChangeContent(_ controller: NSFetchedResultsController<NSFetchRequestResult>) {
        self.objectWillChange.send()
    }
}


这是我的问题的非常简化的版本,我知道我的DataModel可以正常工作,因为值会更改,并且会调用 self.objectWillChange.send(),但是由于某些原因我的视图未更新....

That's a very simplified version of my problem and I know that my DataModel works because the values change and self.objectWillChange.send() is called, but my View isn't updated for some reason....

推荐答案

我看不到NSObject是问题的根源.问题似乎是您尚未实现 objectWillChange .编译器将使您摆脱困境,但是结果是您的 objectWillChange 不执行任何操作.

I don't see that NSObject is the source of the problem. The problem seems to be that you haven't implemented objectWillChange. The compiler will let you get away with that, but the result is that your objectWillChange does nothing.

这是一个简单的示例,显示了如何使用具有绑定功能的计算属性来配置ObservableObject(即NSObject):

Here's a simple example that shows how to configure an ObservableObject (that is an NSObject) with a computed property whose binding works:

class Thing : NSObject, ObservableObject {
    let objectWillChange = ObservableObjectPublisher()
    var computedProperty : Bool = true {
        willSet {
            self.objectWillChange.send()
        }
    }
}

struct ContentView: View {
    @EnvironmentObject var thing : Thing
    var body: some View {
        VStack {
            Button(self.thing.computedProperty ? "Hello" : "Goodbye") {
                self.thing.computedProperty.toggle()
            }
            Toggle.init("", isOn: self.$thing.computedProperty).frame(width:0)
        }
    }
}

通过点击按钮和开关,您可以看到所有内容都处于活动状态并对视图中的绑定做出了响应.

You can see by tapping the button and the switch that everything is live and responsive to the binding within the view.

这篇关于SwiftUI中的计算(NSObject)属性不会更新视图的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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