I need help

 I am trying to learn to write swift. I am very proficient MS VB, which I have been using for almost 20 years. The Book I am learning from is:

SwiftUI for Masterminds by J.D Gauchat. I have got to chapter 7 with no problem. The exercise I am having a problem with Listing 7-5. The error I am getting is:

Thread 1: Fatal error: No Observable object of type ApplicationData found. A View.environmentObject(_:) for ApplicationData may be missing as an ancestor of this view. I have spent the last 2 days rechecking my code. The MacBook I am using was purchased in May this year, is 16 inch, Max 4 pro chip with 128 G Ram I am seeking help here as a last resort, I understand everybody is busy. The main issue I have with the book is the author assumes you know as much as he does. As I stated, I am very proficient MS VB

Firstly Is the SwiftUI for Masterminds to advanced for? Secondly What is the best book to start with and then move on to SwiftUI for Masterminds.

Thirdly I thank you for reading my first post here.

Regards

Terry Harrison

Difficult to help without seeing any code, but that error generally means the View you're seeing the error in has no link back to the ApplicationData object that the View is trying to use.

For example:

// ApplicationData.swift
@Observable
class ApplicationData {
	var someInteger: Int = 1
}

This creates an Observable object called ApplicationData. You can then use it with:

// MainApp.swift
@main
struct MainApp: App {
  var body: some Scene {
    WindowGroup {
      let applicationData: ApplicationData = ApplicationData()
      ContentView()
        .environment(applicationData)
    }
  }
}

// ContentView.swift
struct ContentView: View {
  @Environment(applicationData) var applicationData
  var body: some View {
    Text("someInteger = \(applicationData.someInteger)")
  }
}

The error you're receiving is suggesting you haven't added something like line 8. That's required to bind the object you instantiated (in MainApp in this example, which is the view's ancestor) to the object you want to use in the View, at line 15.

It wouldn't surprise me if you're using a newer version of Swift than was current when the book was written. It is an evolving language, and not always backwards-compatible.

I need help
 
 
Q