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

SOLVED: What the @ am I doing wrong?

Forums > SwiftUI

@GMacD  

Firstly sorry for the jpeg's but I am coding on an iPad and transferring the code on here is eluding me at the moment!

I haven't been able to get the Form Results to print the data that I input into the Form Builder View.

I am an absolute newbie and without this place I wouldn't have been able to get as far as I have and for this I thank you!

I am not overly confident that these jpeg's will appear so fingers crossed.

FormBuilder

FormResults

Kind Regards

Guy MacDonald

1      

You probably don't need the FormResult struct at all. All it seems to be capable of doing is holding a property with your Observable Object in it, but you can give any of your views that same ability just as easily.

Then, in the FormResultsView struct, you shouldn't be calling the initializer for your ObjervableObject. You only want to call the initializer when you are creating the object for the first time using @StateObject. The way you have it now is creating a brand new object to be used as your @ObservableObject in this View. So any of the data in the original @StateObject you created is not being used in FormResultsView.

Change this line in your FormResultsView struct...

@ObservedObject var cardsOne = CardsOne()

To this instead...

@ObservedObject var cardsOne: CardsOne

But now, you will need to make sure that you are passing your @StateObject into the initializer for FormResultsView when you first create it.

1      

@GMacD  

Thank you so much for your time!

Unfortunately I still have a problem. I actually inserted that struct because it's contents "@ObservedObject var cardsOne: CardsOne"produced the error below when in the main struct as you have advised.

This error leaves me completely baffled so I am hoping you can spare your time to help me get rid of it.

1      

I'm not sure what that error means, but you shouldn't be capitalizing CardsOne when you are passing it in the initializer. You want to pass in the value stored in your @StateObject variable, which is named cardsOne.

FormResultView(cardsOne: cardsOne)

When you capitalize it, it is trying to pass the Type CardsOne (the type itself) as a parameter, rather than the instance of CardsOne that you created.

Does that get rid of the error?

2      

Also, for future reference, it is much easier to help you with things like this if you can copy your code in these forums in a way that we can look at easier and copy and paste it ourselves.

In order to do that on these forums, you will need to type 3 backticks ``` then press the return key twice, and type three more backticks ``` Then you can paste your code on the empty line between the two sets of triple backticks.

Normally this is the buttton at the top left of a keyboard, but on iPad, it is a little bit harder to find. You will have to tap the 123 button, then find the apostrophe key ' at the bottom right of the keyboard. You will have to long press on the apostrophe key to see some other options, and choose the one on the far left.

If you want to, you could go into your iPad settings and go to the keyboard settings to set up a shortcut for this. Maybe make it so whenever you type 3bt it will automatically change it to ``` to type the 3 backticks for you a little bit easier.

2      

@GMacD  

Hello @FlyOstrich

Thank you very much for your help!

Yes it got rid of the error and our code now works! (Yes our code because without your help I don't think I would have found my mistake).

The upside of this is, I understand better the logic of how data is passed from one View to another with a button. I honestly didn't know a button could do that.

A day you learn something is a good day so today is an excellent day!

Thanks again

Guy

2      

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.