Classes are confusing me (Beginner)

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
}
}

@thedaveb If you prefer learning from books, start with the Swift Apprentice which is platform agnostic and uses playgrounds to focus on the Swift language itself:

https://store.raywenderlich.com/products/swift-apprentice

You can then move on to the iOS Apprentice which teaches you how to build iOS apps:

https://store.raywenderlich.com/products/ios-apprentice

If you prefer learning from video courses instead, please check out our iOS learning paths when you get a chance:

https://www.raywenderlich.com/paths

I hope it helps!

This topic was automatically closed after 166 days. New replies are no longer allowed.