You haven't actually wrapped your struct
in a class
as the instructions told you to do. To do that, you simply need to move your struct
definition inside of the curly braces of the class
like this...
class SharedOrder: ObservableObject {
struct Order: Codable {
// all variables previously in the class
}
@Published var data = Order()
}
Am I understanding the reasoning correctly? (Please correct me if I am not)
Yes, that is the reason you do not need to provide custom codable conformance. Because they are all simple data types.
What data types in structs would NOT conform to codable and would require a custom function to encode the data?
Any type of struct
or class
that you create (or that exists in a framework) with a property that is not a simple data type (string, bool, int, double, etc) will not conform to codable on its own. So, if you are working with a struct
that has an Image
property, you will likely need to manually type code that will tell how to convert the image into data that can be written to memory in order for it to conform to Codable.
Why does Paul keep static let types = ["Vanilla", "Strawberry", "Chocolate", "Rainbow"] inside the class?
He likely leaves the array in the class because it isn't really necessary data that needs to be encoded and sent for the order to be processed. It is more of a user interface element that is used to display an understandable label to the user. When you create the struct to hold all of your data about an order, you would want to keep it as simple as possible to encode and send the smallest amount of data that is necessary to know everything that you need to know about an order. In this case, we already have an Int
named type
to represent the user's selection, and don't need the label to be sent along with it for the order to be processed.
Why does Paul keep the CodingKeys enum inside the struct if we're not using them for a custom encoder?
I am not sure about this one, but I don't have access to Paul's finished solution for this project, so I'd have to look at how he did it to get a better idea. I know that you have to used coding keys when working with @Published
properties, but I think that the only published property you would need would be in your class. Are you sure that he isn't including the enum in the class and not the struct?