【问题标题】:Trying to set @published bool to true based on results from an API call尝试根据 API 调用的结果将 @published bool 设置为 true
【发布时间】:2021-06-22 11:30:48
【问题描述】:

首先,我对 swift 和编程非常陌生(来自设计领域)。 我正在尝试根据 posts.count 更新 doesNotificationsExist

我在Api().getPosts {} 中变得真实 我在哪里打印以下内容:

print("Api().getPosts")
print(doesNotificationExist)

但在外面(在loadData() {})我仍然得到错误而不是@Publihed var doesNotificationExist:Bool = false 不会更新。

请帮帮我,我真的很感激一些关于我做错了什么以及我需要做什么的指导。

这是我的代码:

import SwiftUI
import Combine

public class DataStore: ObservableObject {
    @Published var posts: [Post] = []
    @Published var doesNotificationExist:Bool = false
    
    init() {
        loadData()
        startApiWatch()
    }
    
    func loadData() {
        
        Api().getPosts { [self] (posts) in
            self.posts = posts

            if posts.count >= 1 {
                doesNotificationExist = true
            }
            else {
                doesNotificationExist = false
            }
            
            print("Api().getPosts")
            print(doesNotificationExist)
        }
        print("loadData")
        print(doesNotificationExist)
    }
    
    func startApiWatch() {
        Timer.scheduledTimer(withTimeInterval: 60, repeats: true) {_ in
            self.loadData()
            
        }
    }

查看我尝试根据store.doesNotificationsExist设置图像的位置

状态栏控制器:

import AppKit
import SwiftUI

class StatusBarController {
    private var statusBar: NSStatusBar
    private var statusItem: NSStatusItem
    private var popover: NSPopover
    
    @ObservedObject var store = DataStore()
    
    init(_ popover: NSPopover)
    {
        self.popover = popover
        statusBar = NSStatusBar.init()
        statusItem = statusBar.statusItem(withLength: 28.0)
        
        statusItem.button?.action = #selector(togglePopover(sender:))
        statusItem.button?.target = self
    
        if let statusBarButton = statusItem.button {
            let itemImage = NSImage(named: store.doesNotificationExist ? "StatusItemImageNotification" : "StatusItemImage")
            statusBarButton.image = itemImage
            statusBarButton.image?.size = NSSize(width: 18.0, height: 18.0)
            statusBarButton.image?.isTemplate = true
            statusBarButton.action = #selector(togglePopover(sender:))
            statusBarButton.target = self
        }
        
    }
`Other none relevant code for the question`

 }

【问题讨论】:

    标签: swift swiftui observableobject


    【解决方案1】:

    这是一个闭包,希望是@escaping@escaping 用于通知接受闭包的函数的调用者,闭包可能被存储或超出接收函数的范围。因此,您的外部打印语句将首先使用 bool 值 false 调用,一旦 timer 完成,将调用 closure 将您的 Bool 值更改为 是的

    检查下面的代码-:

    import SwiftUI
    
    
    public class Model: ObservableObject {
        //@Published var posts: [Post] = []
        @Published var doesNotificationExist:Bool = false
        
        init() {
            loadData()
           // startApiWatch()
        }
        
        func loadData() {
            
            getPost { [weak self] (posts) in
                //self.posts = posts
    
                if posts >= 1 {
                    self?.doesNotificationExist = true
                }
                else {
                    self?.doesNotificationExist = false
                }
                
                print("Api().getPosts")
                print(self?.doesNotificationExist)
            }
            print("loadData")
            print(doesNotificationExist)
        }
        
        func getPost(completion:@escaping (Int) -> ()){
            Timer.scheduledTimer(withTimeInterval: 5, repeats: true) {_ in
                completion(5)
                
            }
        }
    }
    
    struct Test1:View {
        @ObservedObject var test = Model()
        var body: some View{
            Text("\(test.doesNotificationExist.description)")
        }
    }
    

    【讨论】:

    • 感谢@tushar-sharma 似乎解决了这个问题。 bool 未设置为 true。尽管我似乎仍然有问题。我已编辑我的问题以包含我正在尝试根据 do.NotificationExist 设置图像的其他文件
    • @NiklasPeterson 您分享的更新代码用于 swift,而不是 swiftUI。属性包装器旨在与 swiftUI 视图一起使用。
    • 抱歉还是很新的编程:D所以我不能在正常的swift中使用@ObservedObject var store = DataStore()let itemImage = NSImage(named: store.doesNotificationExist ? "StatusItemImageNotification" : "StatusItemImage")
    • 你应该看看如何在 swift 和 swiftUI 之间进行通信。我们仍然可以这样做,但不像您尝试的那样直接。
    • @Niklas 如果有帮助,请接受并支持答案。
    猜你喜欢
    • 2012-06-19
    • 2012-04-25
    • 1970-01-01
    • 1970-01-01
    • 2015-05-03
    • 1970-01-01
    • 1970-01-01
    • 2013-05-25
    • 2016-09-30
    相关资源
    最近更新 更多