Questions tagged [observableobject]
The observableobject tag has no usage guidance.
210
questions
0
votes
3
answers
56
views
Do I have to use an ObservableObject in SwiftUI?
I would like to use a struct instead of a class as a state for my View, and as you may know, ObservableObject is a protocol only classes can conform to.
Do I have to wrap my struct in a ViewModel or ...
1
vote
1
answer
20
views
How to update observableObject class after value is updated by picker - swiftui
I have an ObservableObject class that fetches the remote data based on month and year value. Initially it works fine because I use current month and year values however I want to update and re run the ...
0
votes
0
answers
41
views
Why can't we use existential types as types of properties wrapped with @ObservedObject
protocol ExampleStore: ObservableObject {
var text: String { get }
}
class ConcreteExampleStore: ExampleStore {
@Published var text: String = ""
}
struct ExampleView: View {
@...
0
votes
2
answers
133
views
ForEach in Scrollview does not get updated when @Published gets changed
My ForEach in a Scrollview does not get updated when CommentViewModel comments get updated. It gets successfully updated, but for some reason, CommentView does not get updated. I have tried everything,...
1
vote
1
answer
33
views
Accessing an enum in ObservableObject
I'm trying to refactor some code I have and thought I'd be able to move some of the code from globally accessible enum into the ViewModel of the app.
However, when I try to access it from the View it ...
0
votes
1
answer
61
views
ObservableObject not updating view in nested loop SWIFTUI
Regarding the following project :
You have an amountSum of 100
When you click on one user "plus" button, this specific user have to pay this amount but if you click on multiple user "...
-1
votes
1
answer
43
views
@AppStorage inside ObservableObject - How is `objectWillChange` triggered from inside the property wrapper?
In iOS 14.5, Apple made a change to @AppStorage so that it can be properly used within an ObservableObject:
AppStorage property wrappers now work as expected when contained inside an ObservableObject,...
1
vote
2
answers
42
views
SwiftUI: Must an ObservableObject be passed into a View as an EnvironmentObject?
If I create an ObservableObject with a @Published property and inject it into a SwifUI view with .environmentObject(), the view responds to changes in the ObservableObject as expected.
class ...
0
votes
1
answer
81
views
Reset `ObservableObject` properties after logout
I have one ObservableObject class for managing login data:
final class LoginData: ObservableObject {
// MARK: - Properties
@Published var otpVerificationType: OTPVerificationType = .mobileNumber
@...
-1
votes
1
answer
61
views
Why does @Published not work in NSManagedObject?
@Published publishes correctly when used in an ObservableObject. But does not seem to publish when used in an NSManagedObject (which conforms to ObservableObject).
In the following example when using ...
2
votes
1
answer
86
views
Detail View from different Data Model (API response) in swiftUI
Elaborating the Problem in depth with code
I have a data model (API response) using which I am creating a list. The list items should also display their details in detail view. The problem is details ...
0
votes
1
answer
57
views
How to read property from a struct when being in @ObservableObject
I don't know how to read a property that is in a struct from a class that is an Observable Object.
Context:
I'm trying to build an app which consists of 2 views:
a custom calendar;
a popup with a ...
0
votes
0
answers
50
views
Use Swift ObservableObject to change view label when UserSettings change
I’ve created a small sample project in Swift Playgrounds to debug an issue I’ve encountered. This sample project contains the a primary ContentView with a single Text field and a button that opens ...
0
votes
0
answers
60
views
How Do I Make Changes To Child Model Entities Update SwiftUI
I'm trying to implement a UI for a classic parent-children (master-details, etc.) model. All works fine when I have a direct reference to the children in the view, but when I introduce a view-model ...
0
votes
1
answer
48
views
Dynamically changing colors with @State or @ObservableObject using static functions in Swift
I have a SwiftUI view with a few text fields at the top, and I am trying to dynamically change the color of these. The best option I have found so far is to keep a @State variable that holds an array ...
0
votes
1
answer
44
views
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 ...
1
vote
2
answers
166
views
SwiftUI Bind to @ObservableObject in array
How do I pass a bindable object into a view inside a ForEach loop?
Minimum reproducible code below.
class Person: Identifiable, ObservableObject {
let id: UUID = UUID()
@Published var healthy: ...
1
vote
1
answer
29
views
Passing variable from view inside the class ObservableObject?
i got this struct
struct Calendar: View {
@State private var selectDate = Date()
var body: some View {
// my code
}
Class fetchmydate: ObservableObject {
func fetchmydata (){
// i want to ...
0
votes
1
answer
70
views
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 ...
0
votes
1
answer
79
views
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 ...
1
vote
0
answers
30
views
How to get @Published updates in Combine with a Nested ObservableObject Array (Replicate ValueType Behavior)
I'm not really sure how to properly title the question, so hopefully it is made clear here.
the use case is mostly hypothetical at the moment, but if I have an array of published objects where Child ...
-1
votes
1
answer
172
views
Thread 1: Fatal error: No ObservableObject of type SharedDataModel found. A View.environmentObject may be missing as an ancestor of this view
I have another project with almost identical code and it runs with no issues i'm not sure why it crashes. It build successfully then it crashes with Fatal error on the if statement line.
if ...
0
votes
1
answer
315
views
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 ...
0
votes
0
answers
54
views
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 ...
2
votes
2
answers
426
views
SwiftUI: Binding on property of struct derived from environment object
I have two structs in my task manager app:
struct Task {
var identifier: String
var title: String
var tags: [String] // Array of tag identifiers
}
struct Tag {
var identifier: String
...
0
votes
0
answers
60
views
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,
"...
1
vote
1
answer
104
views
SwiftUI: State variable from observed singleton property leads to error
I use a singleton to access subscription offerings from RevenueCat as an @ObservedObject on various views in the app:
import Foundation
import Purchases
import SwiftUI
class SubscriptionManager: ...
0
votes
1
answer
43
views
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 ...
0
votes
1
answer
107
views
Timer within EnvironmentObject view model not updating the View
I have a view model, that has multiple child view models. I am fairly new to watchOS, SwiftUI and Combine - taking this opportunity to learn.
I have a watchUI where it has
Play Button (View) - ...
1
vote
1
answer
342
views
SwiftUI View not updating with binding to computed property on ObservableObject
I have a very simple SwiftUI view that only shows a TextField. The text field's text is bound to the string property of my viewModel that I instantiate as a @StateObject:
struct ContentView: View {
...
1
vote
1
answer
84
views
Forward/chained properties from service to model using @published, @StaticObject etc. in SwiftUI
Trying to go from UIKit to SwiftUI I keep wondering how to apply a service layer properly and use observables to publish updates from the service layer all the way out through the view model and to ...
7
votes
0
answers
283
views
Why is didSet called twice on the TextField binding in SwiftUI?
I have a very basic view that only shows a TextField:
View
struct ContentView: View {
@StateObject var viewModel = ViewModel()
var body: some View {
TextField("Enter a ...
0
votes
0
answers
53
views
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 ...
0
votes
0
answers
106
views
Why Animation doesn't work inside a child view passed by Binding?
Edit:
I found a solution but I would really like to know why it behaves this way as I got the solution by simply trial and error.
The solution I found is to add .animation(.default, value: user.times) ...
0
votes
1
answer
27
views
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 ...
1
vote
0
answers
39
views
How can SwiftUI Views change if the updated variable isn’t marked with a $?
In many cases in SwiftUI, values are marked with a $ to indicate that they’re a Binding variable, and allow them to update dynamically. Here’s an example of this behavior:
class Car: ObservableObject {...
0
votes
1
answer
282
views
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 ...
-2
votes
1
answer
31
views
How to transfer data between Class/func and View
I decoded json data from an API and now I want to transfer the decoded data to my view where it can be displayed.
the problem is that I cannot transfer the decoded data to my view with @Observable ...
0
votes
1
answer
44
views
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, ...
0
votes
1
answer
105
views
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 ...
0
votes
1
answer
401
views
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 ...
0
votes
0
answers
69
views
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 ...
0
votes
2
answers
94
views
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 {
...
3
votes
0
answers
129
views
SwiftUi 3.0 - @EnvironmentObject causing NavigationLink to pop
I've encountered a very odd bug in one of my apps on iOS 15 / SwiftUi 3.0 - can't find any info on it.
I have a series of 3 screens that each link to one another with a tag/selection NavigationLink as ...
1
vote
1
answer
64
views
Binding variable from an ObservableObject cannot produce animation when changing the value
I have a List view created from an integer array called data and use binding to pass the data to the view. I am using withAnimation when appending elements to the data array.
If the property wrapper ...
0
votes
1
answer
245
views
Computed Property from Child Struct Not Updating in SwiftUI
I have a data model in my SwiftUI app that looks something like this:
struct Entry: Identifiable{
var id = UUID().uuidString
var name = ""
var duration: Int{
//An SQLite query ...
0
votes
1
answer
59
views
watchOS didUpdateLocation freezes during location change
I am developing a running app in SwiftUI for watchOS. The problem is that when I start the run, the measuring works fine as 0.55 0.56 0.57 km, etc. Then it randomly freezes, and jumps from 0.57 ...
0
votes
1
answer
68
views
Observed Object gets Dismissed Unintentionally SwiftUI
I have an HStack in a NavigationView with the following design:
User can scroll, then tap an item from the HStack list to see details
On Detailed View user can select the item
if item is selected, I ...
2
votes
1
answer
479
views
Binding in SwiftUI ForEach from Parent to Child
I have a Subtask defined like this:
struct Subtask: Identifiable{
var id = UUID().uuidString
var text = ""
}
I populate an array of them in an ObservableObject like this:
//View Model
...
0
votes
1
answer
43
views
Missing arguments for parameters 'language', 'date' in call
I have an observable object:
class Schedule: ObservableObject {
@Published var language: Language?
@Published var date = Date()
init(language: Language, date: Date) {
self....