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

SOLVED: Day 50: CupcakeCorner - Defining order

Forums > 100 Days of SwiftUI

I'm confused about the way order is defined throughout the three views in CupcakeCorner.


For the first, ContentView, we defined order. @ObservedObject var order = Order()

Then for the second view, AddressView, we defined order but never assigned a value to it, and then we passed in order from the first view. @ObservedObject var order: Order

But then, for the third view, we defined order, and assigned it a value. @ObservedObject var order = Order() To make it more confusing, order is also passed from the second view into the third.


I don't understand why I can't just define order in the third view using @ObservedObject var order: Order and then pass in the variable from the second view. Why do I have to assign it a value? What really confuses me is if I try that, suddenly order doesn't have any of the values I assigned to it in the past views.

If someone could it explain what's happening here, that would be amazing.

Heres a link to the project.

2      

hi,

the code from @twostraws's 100 Days/Project10 source begins this way on CheckoutView:

struct CheckoutView: View {
    @ObservedObject var order: Order
    // and other stuff

and this seems the right way to do things. but the code in your project begins this way:

struct CheckoutView: View {
    @ObservedObject var order = Order()
    // and other stuff

not sure why you did this (can't recall Paul doing this or why he would do it), but i don't think this will hurt: you've simply given var order a default value that will be overriden by the parameter you pass in.

hope that helps,

DMG

2      

TAKE YOUR SKILLS TO THE NEXT LEVEL If you like Hacking with Swift, you'll love Hacking with Swift+ – it's my premium service where you can learn advanced Swift and SwiftUI, functional programming, algorithms, and more. Plus it comes with stacks of benefits, including monthly live streams, downloadable projects, a 20% discount on all books, and free gifts!

Find out more

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.