Observation

RSS for tag

Make responsive apps that update the presentation when underlying data changes.

Posts under Observation tag

110 Posts

Post

Replies

Boosts

Views

Activity

NavigationPath doesn't work with the @Observable macro iOS 17
Hello! I'm not able to push a view into a stack using new @Observable macro. import SwiftUI import Observation @Observable class NavigationModel { var path = NavigationPath() } struct ContentView: View { @State var navigationModel: NavigationModel = NavigationModel() var body: some View { NavigationStack(path: $navigationModel.path) { VStack { Button { navigationModel.path.append("Text") } label: { Text("Go to the next screen") } } .navigationDestination(for: String.self) { item in Text("Pushed view") } } } } Everything works fine when I use ObservableObject with @Published properties: class NavigationModel: ObservableObject { @Published var path = NavigationPath() } struct ContentView: View { @StateObject var navigationModel: NavigationModel = NavigationModel() var body: some View { NavigationStack(path: $navigationModel.path) { Button { navigationModel.path.append("Text") } label: { Text("Go to the next screen") } .navigationDestination(for: String.self) { item in Text("Pushed view") } } } }
1
0
1.9k
Jul ’23
@ObservationIgnored weak var - must be initialised?
I understand that @Observable currently requires all properties to be initialised. However I am surprised that opting out of observation does not work: @Observable class Assessment { var name = "" var processes: [Process] = [] } @Observable class Process { var name = "" @ObservationIgnored weak var parent: Assessment? } The error message is Return from initializer without initializing all stored properties from the Process class. Any suggestions?
0
1
1.1k
Jul ’23
Is it possible to use new Observation Framework with UIKit?
Apple released their new Observation Framework in WWDC23 and they use it with SwiftUI. I've seen lots of resources about how to use it with SwiftUI but I cannot find anything how can I use it with UIKit. So, I am curious about is there any way to use it with UIKit. How we can use the two together? If we cannot, Why? and What are the restrictions of Observation Framework?
2
2
3.0k
Jun ’23
Migrating from the Observable Object protocol to the Observable macro
// Before I was able to do something like: struct ContentView: View { @EnvironmentObject var listData: ListData var body: some View { ListView($listData.listDataArray) } } struct ListView: View { @Binding var listDataArray: [DataType] } class ListData: ObservableObject { @Published var listDataArray: [DataType] = [] } // Now, I will get the error "Cannot find '$listData' in scope" on the line indicated below when I try to migrate to the Observable macro struct ContentView: View { @Environment(ListData.self) var listData var body: some View { ListView($listData.listDataArray) ----------> Error } } struct ListView: View { @Binding var listDataArray: [DataType] } @Observable class ListData { var listDataArray: [DataType] = [] } @main struct SomeApp: App { @State private var listData = ListData() var body: some Scene { WindowGroup { ContentView .environment(listData) } } }
3
0
1.7k
Jun ’23
Throttling and Debouncing
I want to use Throttling and Debouncing to handle the inputs from the user. These are common techniques to reduce the number of events that are triggered by the inputs. I know how to implement them with the ObservableObject protocol and Combine framework. However, I am trying to migrate to the @Observable property wrapper, which is a new feature of SwiftUI. I cannot find a way to support Throttling and Debouncing with @Observable. Does anyone have any ideas or suggestions on how to solve this problem?
1
1
1.1k
Jun ’23
ObservableObject protocol to @Observable macro
// Before class A: ObservableObject { @Published var dataArray: [dataType] } class B: ObservableObject { @Published var dataArray: [dataType] init(a: A) { A.$dataArray .assign(to: &$dataArray) } } // After applying @Observable, I get an error "Cannot find 'A.$anArray' in scope" on the line indicated below. @Observable class A { var dataArray: [dataType] } @Observable class B { var dataArray: [dataType] init(a: A) { A.$dataArray ----------------------------- Error .assign(to: &$dataArray) } } Class A contains an array to store data fetched from an API. I'm trying to extract some contents from class A into class B. Making them focus on different functionality. All functions in Class B also require the downloaded data.
1
0
1k
Jun ’23
didSet not longer supported in observable?
I tried to migrate some code to SwiftData and Observable...doing this I wanted to mark a ViewModel-Class with @observable but get errors when using it in combination with didSet. What's wrong with it?! @Observable struct LADVAthletes { var queryName : String = "" { didSet { guard oldValue != queryName else { return } guard queryName.count < 2 else { athletesList.removeAll() return } } } var athletesList : [Athlete] = [Athlete]() } I get multiple Errors saying "Cannot find 'oldValue' in scope" "Instance member 'queryName' cannot be used on type 'LADVAthletes'; .... And so on... What's wrong with it?!
1
1
1.6k
Jun ’23
@Observable and other SwiftUI Macros
The sessions showing off the new SwiftUI macros are missing a key element.... How are they enabled within XCODE?!??!? For instance the session "Discover Observation in SwiftUI" starts with the code block. @Observable class FoodTruckModel { var orders: [Order] = [] var donuts = Donut.all } Well.... Something is missing here! I'm sure it's that I need to import something.... but for an intro session it MIGHT be important to mention what needs to be imported and any other requirements. Because right now all I'm getting is "Unknown attribute 'Observable'" So far this is a VERY disappointing WWDC. I got all excited about VisionOS... Nope, all the SDK isn't being release for a month. Reality Composer Pro... Not out yet. SwiftUI macros... Sure let's dive right in to step 6 and not tell you what you need to get started with this feature.
4
4
3.6k
Jun ’23
NavigationPath doesn't work with the @Observable macro iOS 17
Hello! I'm not able to push a view into a stack using new @Observable macro. import SwiftUI import Observation @Observable class NavigationModel { var path = NavigationPath() } struct ContentView: View { @State var navigationModel: NavigationModel = NavigationModel() var body: some View { NavigationStack(path: $navigationModel.path) { VStack { Button { navigationModel.path.append("Text") } label: { Text("Go to the next screen") } } .navigationDestination(for: String.self) { item in Text("Pushed view") } } } } Everything works fine when I use ObservableObject with @Published properties: class NavigationModel: ObservableObject { @Published var path = NavigationPath() } struct ContentView: View { @StateObject var navigationModel: NavigationModel = NavigationModel() var body: some View { NavigationStack(path: $navigationModel.path) { Button { navigationModel.path.append("Text") } label: { Text("Go to the next screen") } .navigationDestination(for: String.self) { item in Text("Pushed view") } } } }
Replies
1
Boosts
0
Views
1.9k
Activity
Jul ’23
@ObservationIgnored weak var - must be initialised?
I understand that @Observable currently requires all properties to be initialised. However I am surprised that opting out of observation does not work: @Observable class Assessment { var name = "" var processes: [Process] = [] } @Observable class Process { var name = "" @ObservationIgnored weak var parent: Assessment? } The error message is Return from initializer without initializing all stored properties from the Process class. Any suggestions?
Replies
0
Boosts
1
Views
1.1k
Activity
Jul ’23
Is it possible to use new Observation Framework with UIKit?
Apple released their new Observation Framework in WWDC23 and they use it with SwiftUI. I've seen lots of resources about how to use it with SwiftUI but I cannot find anything how can I use it with UIKit. So, I am curious about is there any way to use it with UIKit. How we can use the two together? If we cannot, Why? and What are the restrictions of Observation Framework?
Replies
2
Boosts
2
Views
3.0k
Activity
Jun ’23
Migrating from the Observable Object protocol to the Observable macro
// Before I was able to do something like: struct ContentView: View { @EnvironmentObject var listData: ListData var body: some View { ListView($listData.listDataArray) } } struct ListView: View { @Binding var listDataArray: [DataType] } class ListData: ObservableObject { @Published var listDataArray: [DataType] = [] } // Now, I will get the error "Cannot find '$listData' in scope" on the line indicated below when I try to migrate to the Observable macro struct ContentView: View { @Environment(ListData.self) var listData var body: some View { ListView($listData.listDataArray) ----------> Error } } struct ListView: View { @Binding var listDataArray: [DataType] } @Observable class ListData { var listDataArray: [DataType] = [] } @main struct SomeApp: App { @State private var listData = ListData() var body: some Scene { WindowGroup { ContentView .environment(listData) } } }
Replies
3
Boosts
0
Views
1.7k
Activity
Jun ’23
Throttling and Debouncing
I want to use Throttling and Debouncing to handle the inputs from the user. These are common techniques to reduce the number of events that are triggered by the inputs. I know how to implement them with the ObservableObject protocol and Combine framework. However, I am trying to migrate to the @Observable property wrapper, which is a new feature of SwiftUI. I cannot find a way to support Throttling and Debouncing with @Observable. Does anyone have any ideas or suggestions on how to solve this problem?
Replies
1
Boosts
1
Views
1.1k
Activity
Jun ’23
ObservableObject protocol to @Observable macro
// Before class A: ObservableObject { @Published var dataArray: [dataType] } class B: ObservableObject { @Published var dataArray: [dataType] init(a: A) { A.$dataArray .assign(to: &$dataArray) } } // After applying @Observable, I get an error "Cannot find 'A.$anArray' in scope" on the line indicated below. @Observable class A { var dataArray: [dataType] } @Observable class B { var dataArray: [dataType] init(a: A) { A.$dataArray ----------------------------- Error .assign(to: &$dataArray) } } Class A contains an array to store data fetched from an API. I'm trying to extract some contents from class A into class B. Making them focus on different functionality. All functions in Class B also require the downloaded data.
Replies
1
Boosts
0
Views
1k
Activity
Jun ’23
Why does @Observable need an initial value?
Why would it not allow to set the value in the init statement. The @Model allows this and as I understand it also acts as an @Observable.
Replies
2
Boosts
0
Views
1.8k
Activity
Jun ’23
didSet not longer supported in observable?
I tried to migrate some code to SwiftData and Observable...doing this I wanted to mark a ViewModel-Class with @observable but get errors when using it in combination with didSet. What's wrong with it?! @Observable struct LADVAthletes { var queryName : String = "" { didSet { guard oldValue != queryName else { return } guard queryName.count < 2 else { athletesList.removeAll() return } } } var athletesList : [Athlete] = [Athlete]() } I get multiple Errors saying "Cannot find 'oldValue' in scope" "Instance member 'queryName' cannot be used on type 'LADVAthletes'; .... And so on... What's wrong with it?!
Replies
1
Boosts
1
Views
1.6k
Activity
Jun ’23
"Discover Observation in SwiftUI" session has a bug
They provide an example on using @Observable in minute 5:14 : @Observable class Account { var userName: String? } However, if you put that in Xcode this gives an error: @Observable requires property 'userName' to have an initial value (from macro 'Observable') Anyone else seeing this?
Replies
3
Boosts
1
Views
1.4k
Activity
Jun ’23
@Observable and other SwiftUI Macros
The sessions showing off the new SwiftUI macros are missing a key element.... How are they enabled within XCODE?!??!? For instance the session "Discover Observation in SwiftUI" starts with the code block. @Observable class FoodTruckModel { var orders: [Order] = [] var donuts = Donut.all } Well.... Something is missing here! I'm sure it's that I need to import something.... but for an intro session it MIGHT be important to mention what needs to be imported and any other requirements. Because right now all I'm getting is "Unknown attribute 'Observable'" So far this is a VERY disappointing WWDC. I got all excited about VisionOS... Nope, all the SDK isn't being release for a month. Reality Composer Pro... Not out yet. SwiftUI macros... Sure let's dive right in to step 6 and not tell you what you need to get started with this feature.
Replies
4
Boosts
4
Views
3.6k
Activity
Jun ’23