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

Day 52. Challenge

Forums > 100 Days of SwiftUI

Hi! Have some questions on the challenge. Task #3

  1. How would you name the class and struct in such case?! We had class 'Order'. To name struct 'Order'? So, how I name class? The point of naming classes/ structs properly is not that trivial for me; naming classes especially.

  2. What if I use an array @Published var items = [Item]() , instead of single item var item = Item()?

  3. Why both struct and class had to conform to codable? (Otherwise it doesn't compile)

  4. There's 4th question down below.

Some thoughts: This approach seemed to me more convenient from the point of CodingKeys, encode and decode. (less code to write)

I had to change a lot of things inside Views. basically it was just inserting 'item' everywhere after 'order'. order.item.quantity . instead of order.quantity. In the case of Order.types it has to be changed to Item.types because of static belongs to the type itself.

struct Item: 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 zip = ""

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

        return true
    }

    var cost: Double {
        var cost = Double(quantity) * 2 // i.e. $2 per cake
        cost += Double(type) / 2 // making complicated cakes cost more

        if extraFrosting {
            cost += Double(quantity) // $1 per cake for extra frosting
        }

        if addSprinkles {
            cost += Double(quantity) / 2 // $0.5 per cake for sprinkles
        }

        return cost
    }

}

class Order: ObservableObject, Codable {
    enum CodingKeys: CodingKey {
        case item
    }

    @Published var item = Item()
    //@Published var items = [Item]()

    init( ) { }

    // pretty much the reverse of encode
    required init(from decoder: Decoder) throws {
        let container = try decoder.container(keyedBy: CodingKeys.self)

        item = try container.decode(Item.self, forKey: .item)
    }

    func encode(to encoder: Encoder) throws {
        var container = encoder.container(keyedBy: CodingKeys.self)

        try container.encode(item, forKey: .item)
    }
}
  1. If I create in two separate Views reference to the class, do 'order' and 'myOrder' (in example above) are referencing to the same instance of a class, or they are references to different instances of a class?

    struct ContentView: View {
    @ObservedObject var order = Order()
     ....
    }
    
    struct CheckoutView: View {
      // instead of @ObservedObject var order: Order
      @ObservedObject var myOrder = Order()
      .....
    }

3      

BUILD THE ULTIMATE PORTFOLIO APP Most Swift tutorials help you solve one specific problem, but in my Ultimate Portfolio App series I show you how to get all the best practices into a single app: architecture, testing, performance, accessibility, localization, project organization, and so much more, all while building a SwiftUI app that works on iOS, macOS and watchOS.

Get it on Hacking with Swift+

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.