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

Day 52: Problem with Challenge #3

Forums > 100 Days of SwiftUI

Hi Everyone!

I'm trying to complete the challenge 3 of day 52, which requires to convert the Class structure to a Struct one to better manage the Codable conformance. However, I'm pretty stuck: I added the properties inside a new struct called Test which conforms to Codable, and added this Struct in the Class wrapper as follows:

class Order: ObservableObject, Codable {

    @Published var items = [Test]()
}

However, Swift reports that the Class does not conform to Decodable or Encodable, and I can't understand how I should add this conformance.

I tried this:

class Order: ObservableObject, Codable {

    @Published var items = [Test]() {
        didSet {
            let encoder = JSONEncoder()
            if let encoded = try? encoder.encode(items) {
                UserDefaults.standard.set(encoded, forKey: "Items")
            }
        }
    }

    init() {
        if let items = UserDefaults.standard.data(forKey: "Items") {
            let decoder = JSONDecoder()
            if let decoded = try? decoder.decode([Test].self, from: items) {
                self.items = decoded
                return
            }
        }
        self.items = []
    }
}

but it didn't work.

Any suggestion?

Thank you!

3      

Nevermind, I solved it after a bit more of study. I just added a simplified version of the CodingKeys structure done in the previous days of this project.

3      

Could you help with chalenge , i cant writte the right func encode. Thanks

3      

I struggled with this one for a while as well but got it to work eventually, this is what I ended up with...

class OrderC: ObservableObject {

    @Published var orderS = OrderS()

}
struct OrderS: Codable {
    static let types = ["Vanilla", "Strawberry", "Chocolate", "Rainbow"]

    var type = 0
    var quantity = 3

    var specialRequestEnabled = false {
        didSet {   
            if specialRequestEnabled == false {
                extraFrosting = false
                addSprinkles = false
            }
        }
    }
    var extraFrosting = false
    var addSprinkles = false

    var name = ""
    var streetAddress = ""
    var city = ""
    var postcode = ""

    var hasValidAddress: Bool {
         if name.trimmingCharacters(in: .whitespaces).isEmpty ||
            streetAddress.trimmingCharacters(in: .whitespaces).isEmpty ||
            city.trimmingCharacters(in: .whitespaces).isEmpty ||
            postcode.trimmingCharacters(in: .whitespaces).isEmpty {
            return false
        }

        return true
    }

    var cost: Double {
        var cost = Double(quantity) * 2

        cost += Double(type) / 2

        if extraFrosting {
            cost += Double(quantity)
        }

        if addSprinkles {
            cost += Double(quantity) / 2
        }

        return cost
    }
}
struct ContentView: View {
    @ObservedObject var orderC = OrderC()

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("Select your cake type", selection: $orderC.orderS.type) {
                        ForEach(0..<OrderS.types.count) {
                            Text(OrderS.types[$0])
                        }
                    }
                    Stepper(value: $orderC.orderS.quantity, in: 3...20) {
                        Text("Number of cakes: \(orderC.orderS.quantity)")
                    }
                }

                Section {
                    Toggle("Special requests", isOn: $orderC.orderS.specialRequestEnabled.animation())
                    if orderC.orderS.specialRequestEnabled {
                        Toggle("Extra frosting", isOn: $orderC.orderS.extraFrosting)
                        Toggle("Add sprinkles", isOn: $orderC.orderS.addSprinkles)
                    }
                }
                Section {
                    NavigationLink(
                        destination: AddressView(orderC: orderC)) {
                        Text("Delivery details")
                    }
                }
            }
            .navigationBarTitle("Cupcake Corner")
        }
    }
}

4      

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.