Intermediate Swift 3 - Part 3: Structures | Ray Wenderlich

In this Intermediate Swift 3 video tutorial you'll learn your first named object type on the road to object oriented programming: the structure.


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/3536-intermediate-swift-3/lessons/3

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

Thanks for the heads up! I’ve updated the projects.

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

@pixelblur: I took a similar approach to yours:

struct TShirt {
    enum Size: String {
        case small, medium, large
        
        func price() -> Int {
            switch self {
            case .small: return 3
            case .medium: return 5
            case .large: return 7
            }
        }
    }
    
    enum Color: String {
        case red, blue, white
        
        func price() -> Int {
            switch self {
            case .red: return 2
            case .blue: return 3
            case .white: return 1
            }
        }
    }
    
    enum Material {
        case regular, organic
        
        func price() -> Int {
            switch self {
            case .regular: return 5
            case .organic: return 10
            }
        }
    }
    
    var size: Size
    var color: Color
    var material: Material
    
    func calculatePrice() -> Int{
        return size.price() + color.price() + material.price()
    }
}

Love it, all! Keep up the awesome, work! Feel free to post more code. It’s great to see different approaches.

I am stuck on the Challenge Solution when the solution does not render an answer for the calculation requested.

Should an answer always be rendered in the Challenge Solutions?

Thank you.

This was my approach :slight_smile:

struct TShirt {
    var size:String!
    var color: String!
    var material:String!

    func calculatePrice(){
    
         let priceMap = ["S":3,"M":5,"L":7,"red":2,"blue":3,"white":1,"regular":5,"organic":10]
    
        guard let sizePrice = priceMap[size], colorPrice = priceMap[color], materialPrice = priceMap[material] else {
               return
         }
    
         print(sizePrice + colorPrice + materialPrice)
    
      }
}

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!

Brian,

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 solved it after studying the problem further.

var tShirt = NewTShirt(size: mediumSize, color: color, material: material)
tShirt.calculatePrice()

hi,

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?

Hi

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

Thanks

~

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? :slight_smile:

Thanks for the great videos! :slight_smile:

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
}

Thanks for this @pritam.hinger now I understand how to utilize the code for different sizes.

struct TShirtProperty {
  var name = ""
  var price = 0
}

var sizeSmall = TShirtProperty(name: "Medium", price: 5)
var sizeMedium = TShirtProperty(name: "Large", price: 7)
var sizeLarge = TShirtProperty(name: "Small", price: 3)
var materialOrganic = TShirtProperty(name: "Organic", price: 10)
var materialRegular = TShirtProperty(name: "Regular", price: 5)
var colorBlue = TShirtProperty(name: "Blue", price: 3)
var colorRed = TShirtProperty(name: "Red", price: 2)
var colorWhite = TShirtProperty(name: "White", price: 1)

struct NewTShirt {
  var size: TShirtProperty
  var color: TShirtProperty
  var material: TShirtProperty
  
  func calculatePrice() -> Int {
    var value = 0
    
    value += size.price
    value += color.price
    value += material.price
    
    return value
  }

}
var tShirtOne = NewTShirt(size: sizeMedium, color: colorRed, material: materialOrganic).calculatePrice()
var tShirtTwo = NewTShirt(size: sizeLarge, color: colorBlue, material: materialRegular).calculatePrice()