# Intermediate Swift 3 - Part 3: Structures | Ray Wenderlich

**URL:** <https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021>\
**Category:** 🎓 Courses & Articles\
**Created:** [October 11, 2016, 3:47pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021 "2016-10-11T15:47:22Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![vegetarianzombie](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vegetarianzombie/32/94022_2.png) [@vegetarianzombie](https://forums.kodeco.com/u/vegetarianzombie)\
**Post date:** [October 11, 2016, 3:47pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/1 "2016-10-11T15:47:23Z")

</div>

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](https://www.raywenderlich.com/3536-intermediate-swift-3/lessons/3)

---

<div class="post-metadata">

**Author:** ![napsi](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/napsi/32/81494_2.png) [@napsi](https://forums.kodeco.com/u/napsi)\
**Post date:** [October 11, 2016, 7:46pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/2 "2016-10-11T19:46:33Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![bdmoakley](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/bdmoakley/32/108772_2.png) [@bdmoakley](https://forums.kodeco.com/u/bdmoakley)\
**Post date:** [October 11, 2016, 8:16pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/3 "2016-10-11T20:16:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![pixelblur](https://s3.amazonaws.com/cdn.raywenderlich.com/community/forum-assets/noun-game-ghost-878668.png) [@pixelblur](https://forums.kodeco.com/u/pixelblur)\
**Post date:** [October 14, 2016, 6:12pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/4 "2016-10-14T18:12:43Z")

</div>

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

```

---

<div class="post-metadata">

**Author:** ![frankieshakes](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/frankieshakes/32/81494_2.png) [@frankieshakes](https://forums.kodeco.com/u/frankieshakes)\
**Post date:** [October 14, 2016, 11:14pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/5 "2016-10-14T23:14:58Z")

</div>

@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()
    }
}

```

---

<div class="post-metadata">

**Author:** ![vegetarianzombie](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vegetarianzombie/32/94022_2.png) [@vegetarianzombie](https://forums.kodeco.com/u/vegetarianzombie)\
**Post date:** [October 15, 2016, 1:02am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/6 "2016-10-15T01:02:49Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![donwlarson](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/donwlarson/32/81322_2.png) [@donwlarson](https://forums.kodeco.com/u/donwlarson)\
**Post date:** [October 29, 2016, 8:11pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/7 "2016-10-29T20:11:02Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![jonocto](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/jonocto/32/49955_2.png) [@jonocto](https://forums.kodeco.com/u/jonocto)\
**Post date:** [November 1, 2016, 3:17pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/8 "2016-11-01T15:17:22Z")

</div>

This was my approach 🙂

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

```

---

<div class="post-metadata">

**Author:** ![vegetarianzombie](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/vegetarianzombie/32/94022_2.png) [@vegetarianzombie](https://forums.kodeco.com/u/vegetarianzombie)\
**Post date:** [November 4, 2016, 3:37pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/9 "2016-11-04T15:37:39Z")

</div>

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!

---

<div class="post-metadata">

**Author:** ![donwlarson](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/donwlarson/32/81322_2.png) [@donwlarson](https://forums.kodeco.com/u/donwlarson)\
**Post date:** [November 4, 2016, 7:46pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/10 "2016-11-04T19:46:57Z")

</div>

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

```

}

}

---

<div class="post-metadata">

**Author:** ![donwlarson](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/donwlarson/32/81322_2.png) [@donwlarson](https://forums.kodeco.com/u/donwlarson)\
**Post date:** [November 20, 2016, 8:16pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/11 "2016-11-20T20:16:09Z")

</div>

I solved it after studying the problem further.

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

---

<div class="post-metadata">

**Author:** ![dkliman](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/dkliman/32/69829_2.png) [@dkliman](https://forums.kodeco.com/u/dkliman)\
**Post date:** [April 21, 2017, 7:42am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/12 "2017-04-21T07:42:48Z")

</div>

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?

---

<div class="post-metadata">

**Author:** ![lohanrakesh](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/lohanrakesh/32/81494_2.png) [@lohanrakesh](https://forums.kodeco.com/u/lohanrakesh)\
**Post date:** [May 19, 2017, 12:04pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/13 "2017-05-19T12:04:55Z")

</div>

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

~

---

<div class="post-metadata">

**Author:** ![surayashivji](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/surayashivji/32/89383_2.png) [@surayashivji](https://forums.kodeco.com/u/surayashivji)\
**Post date:** [June 17, 2017, 7:27pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/14 "2017-06-17T19:27:18Z")

</div>

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? 🙂

Thanks for the great videos! 🙂

---

<div class="post-metadata">

**Author:** ![pritam.hinger](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/pritam.hinger/32/70412_2.png) [@pritam.hinger](https://forums.kodeco.com/u/pritam.hinger)\
**Post date:** [June 21, 2017, 5:09pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/15 "2017-06-21T17:09:12Z")

</div>

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  
}

---

<div class="post-metadata">

**Author:** ![cajjan](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/cajjan/32/69668_2.png) [@cajjan](https://forums.kodeco.com/u/cajjan)\
**Post date:** [July 13, 2017, 3:30pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-3-structures-ray-wenderlich/21021/16 "2017-07-13T15:30:18Z")

</div>

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()

```
