TEAM LICENSES: Save money and learn new skills through a Hacking with Swift+ team license >>

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


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?


@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 :/


@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..


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.


Hacking with Swift is sponsored by Superwall.

SPONSORED Superwall lets you build & test paywalls without shipping updates. Run experiments, offer sales, segment users, update locked features and more at the click of button. Best part? It's FREE for up to 250 conversions / mo and the Superwall team builds out 100% custom paywalls – free of charge.

Learn 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.