SwiftUI · State & Binding: Part 2 | raywenderlich.com


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/4001741-swiftui/lessons/23

Just downloaded the materials for this lesson and I get the following error:

Assets.xcassets:1:1: The file “people-left-large@2x-d4415bd976ca5f8d78c2ae88c3948e15.png” for the image set “Team” does not exist.

When I look in the asset catalog folder, indeed, there is no file in the Team folder, but there are PNGs in the other folders for the other images.

Also, for the materials for this lesson, none of the view render in preview.

Hey Moebius, thanks so much for asking this question. The images are located in the Preview Assets where they are used only for rendering in the simulator, which is what we want for this situation. If you were shipping a production app, these would not be included in the app’s main bundle. However they absolutely should be rendering in Preview mode in a simulator. I’ve just double checked there and I’m seeing them render just fine in the sim. I can also locate the file in the folder. Can you just double-check that they’re not there and when running the app in a simulator please? I really appreciate it. Laurie.

Hey Isaac, could you please explain why did you add @ObservedObject to user property in BooksView and BookRow structures? I’ve removed both of them and app works as if were before

SOLVED? I have been running the app in the iPhone 11 Pro simulator. When I switched to iPhone 11 Pro Max, everything started working. Not sure why, but there appears to be something wrong with the BookRow UI on iPhone 11 Pro.

Also getting this error reported by the SwiftUI Preview:
PotentialCrashError: P03E03-Complete.app may have crashed

P03E03-Complete.app may have crashed. Check ~/Library/Logs/DiagnosticReports for any crash logs from your application.

==================================

| Error Domain=com.apple.dt.ultraviolet.service Code=12 “Rendering service was interrupted” UserInfo={NSLocalizedDescription=Rendering service was interrupted}

When I remove the following lines from ContentView.swift the preview works:

      ScrollView(.horizontal, showsIndicators: false) {
        BooksView(user: self.user, books: $books.value)
      }
        .shadow(color: .black, radius: 10, x: 20, y: 20)

Thanks! We tried to use a variety of sims but we ran into lots of strange issues. I’m sure these will clear up as development continues. However, thanks so much for your input in this course! :hugs:

I’m also noticing missing Preview Assets in the materials. I noticed the same issue on the previous unit too, but had gotten things working by copying the similarly names resources from previous units. For some reason, the pngs are missing for ARKit, Metal, RxSwift, SwiftUI, Team, and Vapor. When restored from other units, then it works again.

I’m really enjoying this deep dive into SwiftUI–great stuff so far!

I’m still a bit fuzzy on this bit around @State vs. @EvironmentObject though. I’m not sure I get the difference yet. I’ve seen various blog posts about these various scopes, but I can’t seem to get the differentiation, and I feel like we just demonstrated how @State and @EnvironmentObject are semi-interchangeable give the app worked with the @State modifier just fine until we changed it at the very end–even though we were changing multiple views that were dependent on user.

What’s the litmus test that tells us when to use each?

The projects were updated with the full book list asset files. Other learners have reported issues though so I’m beginning to wonder if there is a rogue asset somewhere! All project seem to render locally for me which is strange :thinking:
Laurie

State is for managing data related to that specific View - Environment Objects are for passing app state around just about anywhere and can even be shared by multiple views.

@isaacmendez Can you please help with this when you get a chance? Thank you - much appreciated! :]

@pandabear42 we used observed object to subscribe to changes in a reference type model. You use that attribute when mutating reference type models need to be monitored for updates and you use the @State attribute when you are using value types such as structs.