UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

How handling model errors and pass it to SwiftUI view

Forums > SwiftUI

I will describe in more detail my interest. I have a view that show list of users (class User). In the same view I described a method for obtaining json and serializing data. Question: It seems to me more correct to encapsulate the method of obtaining data in my User class, but if I do this, then how do I translate errors from this model methods into a view?

3      

If you make your Class conform to ObservableObject then using the @Published proptery wrapper for your errors work? That's the first solution that comes to my mind when reading your question. Here is a referece: https://www.hackingwithswift.com/quick-start/swiftui/what-is-the-observedobject-property-wrapper

-Dan

3      

Are you doing something similar to what Paul describes in https://www.hackingwithswift.com/example-code/language/how-to-convert-json-into-swift-objects-using-codable ?

You might like to lokk at John Sundell's take on this same subject at https://www.swiftbysundell.com/articles/customizing-codable-types-in-swift/

Neither of these sends failures to a View, just report it to the debugging code or just fail. To do that you might like to look at https://www.hackingwithswift.com/articles/161/how-to-use-result-in-swift

which seems to me to be what you are looking for, although I might still not understand your problem.

Good luck

3      

Thank you for advices. I think @Published proptery wrapper for errors this is solution.

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.