尝试根据 API 调用的结果将 @published bool 设置为 true
Trying to set @published bool to true based on results from an API call
大家好,我是 swift 和编程(来自设计领域)的新手。
我正在尝试根据 posts.count
更新 doesNotificationsExist
我在 Api().getPosts {}
中变得真实
我在哪里打印以下内容:
print("Api().getPosts")
print(doesNotificationExist)
但在外面(在 loadData() {}
中)我仍然得到 false 而不是 @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`
}
这是一个 闭包,希望是 @escaping
。 @escaping
用于通知采用闭包的函数的调用者闭包可能已存储或以其他方式超出接收函数的范围。因此,您的外部打印语句将首先使用 bool 值 false 调用,一旦 timer 完成,closure
将被调用更改您的Bool
值为 true.
检查下面的代码-:
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)")
}
}
大家好,我是 swift 和编程(来自设计领域)的新手。 我正在尝试根据 posts.count
更新 doesNotificationsExist我在 Api().getPosts {}
中变得真实
我在哪里打印以下内容:
print("Api().getPosts")
print(doesNotificationExist)
但在外面(在 loadData() {}
中)我仍然得到 false 而不是 @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`
}
这是一个 闭包,希望是 @escaping
。 @escaping
用于通知采用闭包的函数的调用者闭包可能已存储或以其他方式超出接收函数的范围。因此,您的外部打印语句将首先使用 bool 值 false 调用,一旦 timer 完成,closure
将被调用更改您的Bool
值为 true.
检查下面的代码-:
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)")
}
}