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

Day 52 - Cupcake challenge ( Display from structure )

Forums > 100 Days of SwiftUI

Hello everyone!

So I've created a new structure called OrderModel

struct OrderModel: Codable {
    var type: Int
    var quantity: Int

    var extraFrosting = false
    var addSprinkles = false

    var name: String
    var streetAddress: String
    var city: String
    var zip: String

    var specialRequestEnabled = false {  ...  } 
    var cost:  Double { ... }

    static let types = ["Vanilla", "Strawberry", "Chocolate", "Rainbow"]
}

This is what my class looks like:

class OrderViewModel: ObservableObject, Codable {

    @Published var order = OrderModel() // Missing argument for parameter 'from' in call 

    enum CodingKeys: CodingKey {
        case order
    }

    func encode(to encoder: Encoder) throws {
        var containter = encoder.container(keyedBy: CodingKeys.self)
        try containter.encode(order, forKey: .order)

    }

    required init(from decoder: Decoder) throws {
        let containter = try decoder.container(keyedBy: CodingKeys.self)
        order = try containter.decode(order.self, forKey: .order)

    }

    init() {}
}

I do not really know how to display this data now..

struct CupCake: View {
    @StateObject var vm = OrderViewModel()

    var body: some View {
        NavigationView {
            Form {
                Section {
                    Picker("Select your cake type", selection: $vm.order.type ) {
                        ForEach(OrderModel.type.indices) {
                            Text(OrderViewModel.types[$0])
                        }
                    }

                    Stepper("Number of cakes: \(order.quantity)", value: $order.quantity, in: 3...20)
                }

I have tried with :

Picker("Select your cake type", selection: $vm.order.type ) { ... }
Picker("Select your cake type", selection: $OrdelModel.type ) { ... } 

2      

@TooMaa is scratching his head....

I do not really know how to display this data now.

I like to think of ForEach as a view factory!
See -> View Factory
or See -> View Factory
or See -> View Factory

So, ask yourself: What is my ForEach view factory trying to make?

ForEach(OrderModel.type.indices) {
    Text(OrderViewModel.types[$0])  // <-- What is the ForEach view factory trying to make?
}

Answer: Your view factory is trying to make a Text view for each type in your OrderModel.

WAIT WHUT???!?!?

Each type in your what?

Please examine your OrderModel closely. Have you defined "type" in your OrderModel?

Also note: type is probably a very bad name to use because of its definition within the Swift language. Instead, be more verbose to capture the nature of this variable. Perhaps cupcakeType might be a better (less confusing?) name!

Keep coding. Please let us know how you solved this.

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.