Post

Replies

Boosts

Views

Activity

Reply to How is my variable holding two different objects when it's not an array?
The problem was that the JSON data wasn't loaded before I ran the code to check the current date. I moved that code from the onAppear in the ContentView to the following. DispatchQueue.main.async { self?.user = user self?.user.checkDate() } The checkDate() function is what called that newMonth() function. That's why it was printing two different months. It would print the default value for the User struct, and then once the data was loaded, it would print the month that was stored.
Jun ’21
Reply to When I create an object, the view is updated, but when I update data inside that object, the view is not updated
Well, I resolved it not long after posting. I had to make PostContainer a struct and update the containers themselves using the index. See example below. if var index = self.containers.firstIndex(where: {$0.subreddit == subreddit}) { // Update previous container self.containers[index].timestamp = Date() self.containers[index].posts = posts self.containers[index].active = true print("Updated old container") } else { // Create the container // Set to active, because its a new query let container = PostContainer(timestamp: Date(), subreddit: subreddit, posts: posts, type: .search, active: true) self.containers.append(container) print("Created new search container") } I guess because with structs, you're creating a new version of the object when it's updated and @Published is able to see the change. With class, it seems to struggle to know if there's been a change or not.
Nov ’23