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

SOLVED: Project 10 doesnt work. Decoded JSON only shows default data and not the new entered data?

Forums > 100 Days of SwiftUI

Hello dear friends.

I am new here and I just completed project 10. I can say I understand it good with one exception. When I pick decode my JSON file and try to read it in the alert message in the end it will show me "You ordered 3 vanilla cupcakes"(the default values) even tho I picked Chocolade and 5 of them.

I tried to do a test where I print out in the alert the local values and it works good. It does store properly my entered values but in the step where I encode and decode the data it prints out only the default information. I checked the code several times and its the same as Paul's.

Also I noticed the only diference between me and Paul's code is that in the Order class for me:

required init(from decoder: Decoder) throws {
    let container = try decoder.container(keyedBy: CodingKeys.self)

    let type = try container.decode(Int.self, forKey: .type)
    let quantity = try container.decode(Int.self, forKey: .quantity)

    let extraFrosting = try container.decode(Bool.self, forKey: .extraFrosting)
    let addSprinkles = try container.decode(Bool.self, forKey: .addSprinkles)

    let name = try container.decode(String.self, forKey: .name)
    let streetAddress = try container.decode(String.self, forKey: .streetAddress)
    let city = try container.decode(String.self, forKey: .city)
    let zip = try container.decode(String.self, forKey: .zip)
}

It shows a yellow warning for each of the variables in this method. "Initialization of immutable value 'type' was never used; consider replacing with assignment to '_' or removing it".

Cant say I understand this warning. I didnt see it apear in Paul's video even tho our code is the same.

Hope someone can help me. Thank you guys

2      

Hi! Not fully understand why you need init from decoder here. As far as I remember you sent request to server and receive the same request back where you decode it and show as alert, right? In your code where do you get json? URL request of locally?

2      

@ygeras hello and thank you for the reply!

I am not sure myself why he used the init for decoder here. I did not do any changes compared to him in the video but for me it doesnt work. And yes true I did send a request to a server and the response I get back I decode and print it out in an alert message. But for some reason it doesnt work properly. Not sure why and I wasted 1 whole day already but no luck :/

2      

@ygeras I solved it. In the init decoder function I put let on every variable and it shouldnt be there. When I removed it it worked fine. As to why he used the init for decoding I am not. sure myself..

2      

Ok got it. Just looked back at the project. As for why: he uses @Published wrapper and if you add Codable protocol it doesn't understand how to encode/decode so we have to "help" it with that.

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.