Questions tagged [observableobject]
262 questions
0
votes
1 answer
How do you edit an ObservableObject’s properties in SwiftUI from another class?
I’m looking for the proper pattern and syntax to address my goal of having an ObservableObject instance that I can share amongst multiple views, but while keeping logic associated with it contained to another class. I’m looking to do this to allow…

Shackleford
- 485
- 1
- 5
- 16
0
votes
1 answer
Crash when deleting an item from List in SwiftUI with custom RandomAccessCollection
Basic approach
I am currently tring to clean up my Core Data/SwiftUI code, and part of that is moving code out of my Views and into my ViewModels. Since it seems that @FetchRequest does not work in VMs, I tried to create something similar that works…

BlackWolf
- 5,239
- 5
- 33
- 60
0
votes
1 answer
Swift Class Weak Var Init
Is there another way to initialize an already instantiated weak var optional in a class, other than passing it through the parameter? When dealing with multi-page forms, you end up having to instantiate on the previous page, just so you can pass it…

kittonian
- 1,020
- 10
- 22
0
votes
1 answer
Swiftui - Calling Coredata in ObservableObject
I am new to SwiftUI and one thing I am struggling to understand is how do we call CoreData in ObservableObject?
I have the following code in place.
SimpleTodoModel.xcdatamodeld
Inside there is a simple entities name: Task
Main Application
import…

Ryan Fung
- 2,069
- 9
- 38
- 60
0
votes
0 answers
SwiftUI view does not update when NSManagedObject is updated from CloudKit
I have a SwiftUI app with CoreData+CloudKit integrated. My data model is as follows:
MainElement <->> SubElement <-> SubElementMetadata
Each MainElement has one or more SubElements and each SubElement has exactly one SubElementMetadata.
Now I have…

BlackWolf
- 5,239
- 5
- 33
- 60
0
votes
0 answers
ObservableObject And Switching between views
am a beginner in swiftui, still learning how to deal with ObservableObject
let me show my code and illustrate what is my question...
JsonResponse as follows:
{
"error" : false,
"user" : {
"username" : "Maxwell",
"id" : 84560,
"name"…

Ramy Khaled
- 15
- 1
- 7
0
votes
1 answer
Why do views from swift package seem to share an ObservableObject instance
My swift package UnsplashSwiftUI has a view called UnsplashRandom that uses a StateObject to asynchronously fetch data from the UnsplashApi. However, if you implement the view twice on the same page they load the same image even though they should…

Arnav Motwani
- 707
- 7
- 26
0
votes
0 answers
How should I share data between ViewModels with SwiftUI?
I am working on a game and want to move the interface logic out of the game logic. The following is what I tried but does not work. What is the correct way to do this?
// Game scene & logic
class GameScene: SKScene, ObservableObject {
…

dooblr
- 458
- 1
- 5
- 18
0
votes
1 answer
Error using replaceSubrange with Array of Doubles: SwiftUI
import Foundation
import CoreData
import SwiftUI
struct dailyModel: Identifiable {
let id: UUID = UUID()
let amount: Double
}
class DailyListModel: ObservableObject {
@Published var dailys: [dailyModel] = []
init() {
…

Fausto IV
- 5
- 1
0
votes
1 answer
SwiftUI View don't see property of ObservableObject marked with @Published
I'm writing my app using SwiftUI and VIPER. And to save the idea of viper(testability, protocols and etc) and SwiftUI reactivity I want to add 1 more layer - ViewModel. My presenter will ask data from interactor and will put in ViewModel, then view…

Igor Ševtšenko
- 223
- 1
- 9
0
votes
1 answer
SwiftUI List requires exact position to render results, so won't render result without [0] etc
When trying to parse some data from a REST API endpoint in SwiftUI, I'm struggling to render out to a list correctly.
In the below example, my List requires the exact array position to render results, so it won't show anything without [0] etc...
I'm…

kejk
- 1
- 2
0
votes
1 answer
Custom Reusable Color Picker not updating colors in other views
The following code successfully shows a set of colors and displays the selected color when the colors are tapped, similar to the standard ColorPicker. What I would like to be able to do is use this custom color picker in other views, in a similar…

fs_tigre
- 10,650
- 13
- 73
- 146
0
votes
1 answer
SwiftUI: How to initialize a new StateObject in a parent view?
I have an app architecture similar to the below (simplified) code. I use a WorkoutManager StateObject which I initialize in the set up view, then pass down to its children via EnvironmentObject. The problem is that upon dismissing the .sheet there…

GarySabo
- 5,806
- 5
- 49
- 124
0
votes
0 answers
Changing an observable object in a binding view will cause the current view reinitializes itself
I have been stuck by this wired problem for a couple of days.
The code is very simple just as below:
struct PlaygroundView: View{
@ObservedObject var obs:OBSObject = OBSObject()
@Binding var…

jeep
- 615
- 2
- 10
- 19
0
votes
2 answers
Notifications not sent when changing an ObservedObject
I have the following code:
class Stuff {
var str: String?
var num = 0
}
class MyStuff:ObservableObject {
@Published var stuff:Stuff?
@Published var numer: Int?
}
class DoSomething {
let observedObject = MyStuff()
var…

user2924482
- 8,380
- 23
- 89
- 173