Hey look like you have the solution to the Challenge inside the BeginningSwift_StructuresChallenge.playground so the BeginningSwift_StructuresChallenge.playground and BeginningSwift_StructuresChallengeSolution.playground are the same
Is it good practice to keep enums in Structs as well? My first inclination is to place all the options into enums so the object would always be created correctly.
struct TShirt {
var size: Size
var color: Color
var material: Material
enum Size: Int {
case S = 3, M = 5, L = 7
}
enum Color: Int {
case Red = 2, Blue = 3, White = 1
}
enum Material: Int {
case Regular = 5, Organic = 10
}
func calculatePrice() {
print("Price is \(size.rawValue + color.rawValue + material.rawValue)")
}
}
You should be able to see all the solutions in the challenge playground – if you aren’t seeing them, or you are stuck, definitely let me know. Cheers!
I don’t see the solution being able to indicate the answer through a call. Shouldn’t there be a final statement resolving to the answer? The example above the Playground’s commented line rendered to an answer.
In the file, "BeginningSwift_StructuresChallengeSolution.playground’ all I see is this:
//: Ub3r H4ck3r Challenge Refactor the code so that materials, color, and sizes are structs with a name and a price. Pass these structs into the TShirt and have it calculate the price
struct TShirtProperty {
var name = “”
var price = 0
}
var mediumSize = TShirtProperty(name: “Medium”, price: 5)
var material = TShirtProperty(name: “Organic”, price: 10)
var color = TShirtProperty(name: “Blue”, price: 3)
struct NewTShirt {
var size: TShirtProperty
var color: TShirtProperty
var material: TShirtProperty
func calculatePrice() → Int {
var value = 0
value += mediumSize.price
value += color.price
value += material.price
return value
I vaguely remember seeing something like what I’m about to mention, but wouldn’t mind being told again how to deal with such a situation.
right now your author can have books, but books also could have multiple authors. what if i want to easily see who the authors of a book are and also see which books an author wrote, without being redundant?
Just wanted to ask how to decide where to use structure and where to use class because its in some cases i can use this interchangeably. Please Suggest
If structs are by default immutable is it best practice to have all properties within a struct be defined with let instead of var? Also, if I’m using a bunch of mutating functions is that indicative that I should probably be using a class instead?
Also, I think there’s a small error in the challenge solution. mediumSize should be size in the calculateSize function at the bottom, right?
Shouldn’t the struct property size be referenced in calculatePrice() function in NewTShirt structure instead of the “mediumSize” variable created outside of the structure.
I Guess this is wrong
func calculatePrice() → Int {
var value = 0
value += mediumSize.price
value += color.price
value += material.price
return value
}
and it should be replaced with
func calculatePrice() → Int {
var value = 0
value += size.price
value += color.price
value += material.price
return value
}