No ObservableObject of type ModelData found: Missing environmentObject, but the environmentObject is actually there (SwiftUI, Xcode) - swift

Xcode version: 14.2
I am trying to display a list of sports clubs. Since my modelData.clubs is still an empty array when just viewing the preview (modelData.clubs gets its actual value in my contentView, when the app is loaded), I would like it to display an array of default clubs if modelData.clubs.isEmpty. However, when I try to run my code, it gives me the following error in the marked line: Thread 1: Fatal error: No ObservableObject of type ModelData found. A View.environmentObject(_:) for ModelData may be missing as an ancestor of this view.
struct ClubList: View {
#EnvironmentObject var modelData: ModelData
#State private var sortByValue = false
var clubs: [Club] = [Club.default, Club.default, Club.default, Club.default, Club.default]
init() {
if (!modelData.clubs.isEmpty) { // ERROR IN THIS LINE
clubs = modelData.clubs
}
if sortByValue {
clubs.sort {
$0.value < $1.value
}
} else {
clubs.sort {
$0.name < $1.name
}
}
}
var body: some View {
NavigationView {
List {
ForEach(clubs) { club in
NavigationLink {
ClubDetail(club: club)
} label: {
ClubRow(club: club)
}
}
}
}
}
}
struct ClubList_Previews: PreviewProvider {
static var previews: some View {
ClubList()
.environmentObject(ModelData())
}
}
ClubList() gets called in my ContentView:
struct ContentView: View {
#EnvironmentObject var modelData: ModelData
// ... //
var body: some View {
if clubsLoaded {
ClubList()
.environmentObject(ModelData())
} // ... //
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(ModelData())
}
}
And ContentView gets called in my App:
#main
struct clubsApp: App {
#StateObject private var modelData = ModelData()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(modelData)
}
}
}
I am pretty new to Xcode, but I think I put the environmentObject(ModelData()) everywhere, where it would be required, and I am pretty lost now on what to do.
Any help would be greatly appreciated!

I think I figured it out!
This post here made me realise that my problem was that I was trying to access my modelData in init(), where it is not possible yet.
Someone from the link said:
The environment is passed down when the body is called, so it doesn't yet exist during the initialization phase of the View struct.
Thus, I solved my problem by moving
if (!modelData.clubs.isEmpty) { clubs = modelData.clubs }
from init() into its own function which is then called in onAppear of my NavigationView.

Related

SwiftUI How to have class viewable from all views

I am fairly new to SwiftUI and I am trying to build an app where you can favorite items in a list. It works in the ContentView but I would like to have the option to favorite and unfavorite an item in its DetailView.
I know that vm is not in the scope but how do I fix it?
Here is some of the code in the views. The file is long so I am just showing the relevant code
struct ContentView: View {
#StateObject private var vm = ViewModel()
//NavigationView with a List {
//This is the code I call for showing the icon. The index is the item in the list
Image(systemName: vm.contains(index) ? "heart.fill" : "heart")
.onTapGesture{
vm.toggleFav(item: index)
}
}
struct DetailView: View {
Hstack{
Image(systemName: vm.contains(entry) ? "heart.fill" : "heart") //Error is "Cannot find 'vm' in scope"
}
}
Here is the code that that vm is referring to
import Foundation
import SwiftUI
extension ContentView {
final class ViewModel: ObservableObject{
#Published var items = [Biase]()
#Published var showingFavs = false
#Published var savedItems: Set<Int> = [1, 7]
// Filter saved items
var filteredItems: [Biase] {
if showingFavs {
return items.filter { savedItems.contains($0.id) }
}
return items
}
private var BiasStruct: BiasData = BiasData.allBias
private var db = Database()
init() {
self.savedItems = db.load()
self.items = BiasStruct.biases
}
func sortFavs(){
withAnimation() {
showingFavs.toggle()
}
}
func contains(_ item: Biase) -> Bool {
savedItems.contains(item.id)
}
// Toggle saved items
func toggleFav(item: Biase) {
if contains(item) {
savedItems.remove(item.id)
} else {
savedItems.insert(item.id)
}
db.save(items: savedItems)
}
}
}
This is the list view...
enter image description here
Detail view...
enter image description here
I tried adding this code under the List(){} in the ContentView .environmentObject(vm)
And adding this under the DetailView #EnvironmentObject var vm = ViewModel() but it said it couldn't find ViewModel.
To put the view model inside the ContentView struct is wrong. Delete the enclosing extension.
If the view model is supposed to be accessed from everywhere it must be on the top level.
In the #main struct create the instance of the view model and inject it into the environment
#main
struct MyGreatApp: App {
#StateObject var viewModel = ViewModel()
var body: some Scene {
WindowGroup {
ContentView()
.environmentObject(viewModel)
}
}
}
And in any struct you want to use it add
#EnvironmentObject var vm : ViewModel
without parentheses.

SwiftUI - Nested links within NavigationStack inside a NavigationSplitView not working

I'm playing around with the new navigation API's offered in ipadOS16/macOS13, but having some trouble working out how to combine NavigationSplitView, NavigationStack and NavigationLink together on macOS 13 (Testing on a Macbook Pro M1). The same code does work properly on ipadOS.
I'm using a two-column NavigationSplitView. Within the 'detail' section I have a list of SampleModel1 instances wrapped in a NavigationStack. On the List I've applied navigationDestination's for both SampleModel1 and SampleModel2 instances.
When I select a SampleModel1 instance from the list, I navigate to a detailed view that itself contains a list of SampleModel2 instances. My intention is to navigate further into the NavigationStack when clicking on one of the SampleModel2 instances but unfortunately this doesn't seem to work. The SampleModel2 instances are selectable but no navigation is happening.
When I remove the NavigationSplitView completely, and only use the NavigationStack the problem does not arise, and i can successfully navigate to the SampleModel2 instances.
Here's my sample code:
// Sample model definitions used to trigger navigation with navigationDestination API.
struct SampleModel1: Hashable, Identifiable {
let id = UUID()
static let samples = [SampleModel1(), SampleModel1(), SampleModel1()]
}
struct SampleModel2: Hashable, Identifiable {
let id = UUID()
static let samples = [SampleModel2(), SampleModel2(), SampleModel2()]
}
// The initial view loaded by the app. This will initialize the NavigationSplitView
struct ContentView: View {
enum NavItem {
case first
}
var body: some View {
NavigationSplitView {
NavigationLink(value: NavItem.first) {
Label("First", systemImage: "house")
}
} detail: {
SampleListView()
}
}
}
// A list of SampleModel1 instances wrapped in a NavigationStack with multiple navigationDestinations
struct SampleListView: View {
#State var path = NavigationPath()
#State var selection: SampleModel1.ID? = nil
var body: some View {
NavigationStack(path: $path) {
List(SampleModel1.samples, selection: $selection) { model in
NavigationLink("\(model.id)", value: model)
}
.navigationDestination(for: SampleModel1.self) { model in
SampleDetailView(model: model)
}
.navigationDestination(for: SampleModel2.self) { model in
Text("Model 2 ID \(model.id)")
}
}
}
}
// A detailed view of a single SampleModel1 instance. This includes a list
// of SampleModel2 instances that we would like to be able to navigate to
struct SampleDetailView: View {
var model: SampleModel1
var body: some View {
Text("Model 1 ID \(model.id)")
List (SampleModel2.samples) { model2 in
NavigationLink("\(model2.id)", value: model2)
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I removed this unclear ZStack and all works fine. Xcode 14b3 / iOS 16
// ZStack { // << this !!
SampleListView()
// }
Apple just releases macos13 beta 5 and they claimed this was resolved through feedback assistant, but unfortunately this doesn't seem to be the case.
I cross-posted this question on the apple developers forum and user nkalvi posted a workaround for this issue. I’ll post his example code here for future reference.
import SwiftUI
// Sample model definitions used to trigger navigation with navigationDestination API.
struct SampleModel1: Hashable, Identifiable {
let id = UUID()
static let samples = [SampleModel1(), SampleModel1(), SampleModel1()]
}
struct SampleModel2: Hashable, Identifiable {
let id = UUID()
static let samples = [SampleModel2(), SampleModel2(), SampleModel2()]
}
// The initial view loaded by the app. This will initialize the NavigationSplitView
struct ContentView: View {
#State var path = NavigationPath()
enum NavItem: Hashable, Equatable {
case first
}
var body: some View {
NavigationSplitView {
List {
NavigationLink(value: NavItem.first) {
Label("First", systemImage: "house")
}
}
} detail: {
SampleListView(path: $path)
}
}
}
// A list of SampleModel1 instances wrapped in a NavigationStack with multiple navigationDestinations
struct SampleListView: View {
// Get the selection from DetailView and append to path
// via .onChange
#State var selection2: SampleModel2? = nil
#Binding var path: NavigationPath
var body: some View {
NavigationStack(path: $path) {
VStack {
Text("Path: \(path.count)")
.padding()
List(SampleModel1.samples) { model in
NavigationLink("Model1: \(model.id)", value: model)
}
.navigationDestination(for: SampleModel2.self) { model in
Text("Model 2 ID \(model.id)")
.navigationTitle("navigationDestination(for: SampleModel2.self)")
}
.navigationDestination(for: SampleModel1.self) { model in
SampleDetailView(model: model, path: $path, selection2: $selection2)
.navigationTitle("navigationDestination(for: SampleModel1.self)")
}
.navigationTitle("First")
}
.onChange(of: selection2) { newValue in
path.append(newValue!)
}
}
}
}
// A detailed view of a single SampleModel1 instance. This includes a list
// of SampleModel2 instances that we would like to be able to navigate to
struct SampleDetailView: View {
var model: SampleModel1
#Binding var path: NavigationPath
#Binding var selection2: SampleModel2?
var body: some View {
NavigationStack {
Text("Path: \(path.count)")
.padding()
List(SampleModel2.samples, selection: $selection2) { model2 in
NavigationLink("Model2: \(model2.id)", value: model2)
// This also works (without .onChange):
// Button(model2.id.uuidString) {
// path.append(model2)
// }
}
}
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}

Trouble getting EnvironmentObject to update the UI

I originally posted another question asking this in the context of a project I was trying to develop, but I can't even get it to work in a vacuum so I figured I'd start with the basics. As the title suggests, my EnvironmentObjects don't update the UI as they should; in the following code, the user enters text on the ContentView and should be able to see that text in the next screen SecondView.
EDITED:
import SwiftUI
class NameClass: ObservableObject {
#Published var name = ""
}
struct ContentView: View {
#StateObject var myName = NameClass()
var body: some View {
NavigationView {
VStack {
TextField("Type", text: $myName.name)
NavigationLink(destination: SecondView()) {
Text("View2")
}
}
}.environmentObject(myName)
}
}
struct SecondView: View {
#EnvironmentObject var myself: NameClass
var body: some View {
Text("\(myself.name)")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(NameClass())
}
}
However, the SecondView doesn't show the text that the user has written, but the default value of name (blank). What am I doing wrong here?
class NameClass: ObservableObject {
#Published var name = ""
}
struct ContentView: View {
#StateObject var myName = NameClass()
var body: some View {
NavigationView {
VStack {
TextField("Type", text: $myName.name)
NavigationLink(destination: SecondView()) {
Text("View2")
}
}
}.environmentObject(myName)
}
}
struct SecondView: View {
#EnvironmentObject var myself: NameClass
var body: some View {
Text("\(myself.name)")
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
.environmentObject(NameClass())
}
}

Passing data from extension in SwiftUI

I am building a complex interface in SwiftUI that I need to break into multiple extensions in order to be able to compile the code, but I can't figure out how to pass data between the extension and the body structure.
I made a simple code to explain it :
class Search: ObservableObject {
#Published var angle: Int = 10
}
struct ContentView: View {
#ObservedObject static var search = Search()
var body: some View {
VStack {
Text("\(ContentView.self.search.angle)")
aTest()
}
}
}
extension ContentView {
struct aTest: View {
var body: some View {
ZStack {
Button(action: { ContentView.search.angle = 11}) { Text("Button")}
}
}
}
}
When I press the button the text does not update, which is my issue. I really appreciate any help you can provide.
You can try the following:
struct ContentView: View {
#ObservedObject var search = Search()
var body: some View {
VStack {
Text("\(ContentView.self.search.angle)")
aTest // call as a computed property
}
}
}
extension ContentView {
var aTest: some View { // not a separate `struct` anymore
ZStack {
Button(action: { self.search.angle = 11 }) { Text("Button")}
}
}
}

SwiftUI - Observable Object initiated from Swift class does not update #ObservedObject on ContentView()

The ObservableObject class is being instantiated from both the ContentView() as well as another Swift class. When a function of the ObservableObject class is run by the Swift class, it does not update the #ObservedObject of the ContentView().
I am aware that this is due to me instantiating the ObservableObject class twice. What is the best practice to utilise #ObservedObject when the Observable Class is not/cannot be instantiated by the ContentView().
I haven't found a way to make #EnvironmentObject work with Swift classes.
I could use a global variable and run a Timer() to check for changes to it. However, this feels like an ugly way to do it?!?
Please see example code below. Please run on a device, to see the print statement.
import SwiftUI
struct ContentView: View {
#ObservedObject var observedClass: ObservedClass = ObservedClass()
// The callingObservedClass does not exist on the ContentView, but is called
// somewhere in the app with no reference to the ContentView.
// It is included here to better showcase the issue.
let callingObservedClass: CallingObservedClass = CallingObservedClass()
var body: some View {
VStack {
// This Text shall be updated, when
// self.callingObservedClass.increaseObservedClassCount() has been executed.
Text(String(observedClass.count))
Button(action: {
// This updates the count-variable, but as callingObservedClass creates
// a new instance of ObservedClass, the Text(observedClass.count) is not updated.
self.callingObservedClass.increaseObservedClassCount()
}, label: {
Text("Increase")
})
}
}
}
class CallingObservedClass {
let observedClass = ObservedClass()
func increaseObservedClassCount() {
// Returning an Int here to better showcase that count is increased.
// But not in the ObservedClass instance of the ContentView, as the
// Text(observedClass.count) remains at 0.
let printCount = observedClass.increaseCount()
print(printCount)
}
}
class ObservedClass: ObservableObject {
#Published var count: Int = 0
func increaseCount() -> Int {
count = count + 1
return count
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
Edit: I suppose my question is how do I get data from a Swift class and update a SwiftUI view when the data changes when I am unable to instantiate the Swift class from the SwiftUI view.
A possible solution to this is to chain the ObservableObject classes. Unfortunately, as of iOS 13.6 this does not work out of the box.
I found the answer via:
How to tell SwiftUI views to bind to nested ObservableObjects
Adjusted & functioning example:
// Add Combine
import Combine
import SwiftUI
struct ContentView: View {
#ObservedObject var callingObservedClass: CallingObservedClass = CallingObservedClass()
var body: some View {
VStack {
// Calling the chained ObservableObject
Text(String(callingObservedClass.observedClass.count))
Button(action: {
self.callingObservedClass.increaseObservedClassCount()
}, label: {
Text("Increase")
})
}
}
}
class CallingObservedClass: ObservableObject {
// Chaining Observable Objects
#Published var observedClass = ObservedClass()
// ObservableObject-chaining does not work out of the box.
// The anyCancellable variable with the below init() will do the trick.
// Thanks to https://stackoverflow.com/questions/58406287/how-to-tell-swiftui-views-to-bind-to-nested-observableobjects
var anyCancellable: AnyCancellable? = nil
init() {
anyCancellable = observedClass.objectWillChange.sink { (_) in
self.objectWillChange.send()
}
}
func increaseObservedClassCount() {
let printCount = observedClass.increaseCount()
print(printCount)
}
}
class ObservedClass: ObservableObject {
#Published var count: Int = 0
func increaseCount() -> Int {
count = count + 1
return count
}
}
struct ContentView_Previews: PreviewProvider {
static var previews: some View {
ContentView()
}
}
I am interested on how to access Swift Class data and update an SwiftUI view if ObservableObject-Chaining is not an option.
Please answer below if you have a solution to this.