Can someone explain to me this code please? I am just learning Swift/SwiftUI but getting frustrated with classes.
I understand what the outcome is but don’t get why it’s written the way it is. Looking on Google to learn about classes and it’s just the same stuff over and over, no one has real world examples.
If there are any course on here that will help then please let me know. I have iOS Apprentice but not looked at it for a while.
import Foundation
class OrderListViewModel: ObservableObject {
@Published var orders = [OrderViewModel]()
init() {
fetchOrders()
}
func fetchOrders() {
Webservice().getAllOrders { orders in
if let orders = orders {
self.orders = orders.map(OrderViewModel.init)
}
}
}
}
class OrderViewModel {
let id = UUID()
var order: Order
init(order: Order) {
self.order = order
}
var name: String {
return self.order.name
}
var size: String {
return self.order.size
}
var coffeeName: String {
return self.order.coffeeName
}
var total: Double {
return self.order.total
}
}