# Intermediate Swift 3 - Part 4: Properties | Ray Wenderlich

**URL:** <https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015>\
**Category:** 🎓 Courses & Articles\
**Created:** [October 11, 2016, 3:44pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015 "2016-10-11T15:44:51Z")\
**Posts on this page:** 20\
**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:44pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/1 "2016-10-11T15:44:51Z")

</div>

In this Intermediate Swift 3 video tutorial you'll learn about the various properties options that you can add to your objects.

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

---

<div class="post-metadata">

**Author:** ![benspektor](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/benspektor/32/81494_2.png) [@benspektor](https://forums.kodeco.com/u/benspektor)\
**Post date:** [October 13, 2016, 6:38am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/2 "2016-10-13T06:38:49Z")

</div>

Hey, great video, but the solution is in the challenge file, thanx for the quick conversion to swift3/Xcode8 though.

---

<div class="post-metadata">

**Author:** ![nbasham](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/nbasham/32/85192_2.png) [@nbasham](https://forums.kodeco.com/u/nbasham)\
**Post date:** [November 30, 2016, 2:29am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/3 "2016-11-30T02:29:50Z")

</div>

This entire series is really nicely done and very informative. One correction to note, computed properties are not read only `(`[Apple docs](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html)`)`. Thanks again.

---

<div class="post-metadata">

**Author:** ![elearner](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elearner/32/109248_2.png) [@elearner](https://forums.kodeco.com/u/elearner)\
**Post date:** [March 29, 2017, 5:09am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/4 "2017-03-29T05:09:41Z")

</div>

Hello Brian,

I need help. I’ve been going through your class and struct videos and need clarification.  
Here’s my **CLASS** , with properties, example that maybe you can help with the details.

**class List**  
**{**  
**var firstStringArray:[String] = []**

**var aStringArray = [“A”,“B”,“C”]**  
**var anotherStringArray = [“X”,“Y”,“Z”]**  
}

If I make an instance of the class:  
**var myList = List()**

And I assign: **myList.firstListArray = myLIst.aStringArray**

Now, I want to reuse and re-assign **firstListArray**.  
I want it to stop pointing or referencing **aStringArray** and now point at  
ONLY **anotherStringArray**.

How do I accomplish this?  
In other words how do I change it’s **“reference”**?

Thanks,

Gary  
PS  
Am I better off using a Struct when my properties are initialized  
to literal values or use a Class like my example?

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [March 29, 2017, 1:23pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/5 "2017-03-29T13:23:48Z")

</div>

In fact, **Array** are implemented as **Struct** , so when you assign an array to another one, you create a copy of it.  
This can be simply verified with the following code :

```
var myList = List()
myList.firstStringArray = myList.aStringArray

print(myList.aStringArray) // PRINTS ["A", "B", "C"]
myList.firstStringArray.removeLast(1)
print(myList.aStringArray) // PRINTS ["A", "B", "C"]

```

The last element of **firstStringArray** was removed, but since it’s a copy of **aStringArray** and not a reference, **aStringArray** isn’t changed.

If you want to use references, you have to encapsulate your array in a “dummy” class, such as :

```
class arrayReference : CustomStringConvertible
{
    var array:[String] = []
    
    convenience init() {
        self.init(array: [])
    }

    init(array:[String])
    {
        self.array = array
    }

    var description: String
    {
        return "\(array)"
    }
}

```

---

<div class="post-metadata">

**Author:** ![elearner](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/elearner/32/109248_2.png) [@elearner](https://forums.kodeco.com/u/elearner)\
**Post date:** [March 29, 2017, 4:27pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/6 "2017-03-29T16:27:39Z")

</div>

Hi Brian,

Great!  
Thanks for all your help.

Gary

---

<div class="post-metadata">

**Author:** ![shelke](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shelke/32/67645_2.png) [@shelke](https://forums.kodeco.com/u/shelke)\
**Post date:** [April 21, 2017, 7:19am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/7 "2017-04-21T07:19:29Z")

</div>

Hi Brian, nice video but i got one doubt here-  
In the [(Apple Docs)] ([Properties — The Swift Programming Language (Swift 5.7)](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Properties.html)) link you have provided,it’s stated that :

‘You must declare computed properties—including read-only computed properties—as variable properties with the _var_ keyword, because their value is not fixed. The _let_ keyword is only used for constant properties, to indicate that their values cannot be changed once they are set as part of instance initialization.’

I understood the need for declaring read-write Computed Properties as variables.But can you please explain me why we need to declare the read-only computed properties as variables?

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [April 26, 2017, 7:16am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/8 "2017-04-26T07:16:56Z")

</div>

If you use a read only computed property, the value returned can change too (even if it cannot explicitly be set), thus you need to use the `var` keyword.

Simple example :

```
class CounterClass {

    private var count = 0    
    var numberOfTimesThisVariableWasUsed : Int{
        get {
            count += 1
            return count
        }
    }
}

let counter = CounterClass()
let value1 = counter.numberOfTimesThisVariableWasUsed
// value1 = 1
let value2 = counter.numberOfTimesThisVariableWasUsed
// value2 = 2

```

The `numberOfTimesThisVariableWasUsed` is a read-only computed property, but this value is changed each time you get it. A `var` keyword is needed since the value can change at runtime.

---

<div class="post-metadata">

**Author:** ![gabriel\_afonso](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/gabriel_afonso/32/67518_2.png) [@gabriel\_afonso](https://forums.kodeco.com/u/gabriel_afonso)\
**Post date:** [May 19, 2017, 3:09am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/9 "2017-05-19T03:09:51Z")

</div>

Hey,

In order to fire the didSet using the initializer you might need to use “defer”

---

<div class="post-metadata">

**Author:** ![rdjfields](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/rdjfields/32/68928_2.png) [@rdjfields](https://forums.kodeco.com/u/rdjfields)\
**Post date:** [June 14, 2017, 2:35pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/10 "2017-06-14T14:35:10Z")

</div>

Brian (@vegetarianzombie),

I was doing the challenge and instead of using if statements I wanted to use a switch statement, but I got errors: “Expression pattern of type ‘Bool’ cannot match values of type ‘Double’”. My code looked like this:

```
struct FuelTank {
    var lowFuel: Bool
    var level: Double {
        didSet {
            let min = 0.0
            let max = 1.0
            let isLowFuel = 0.1
            switch level {
            case level > max :
                level = max
            case level < min :
                level = min
            case level <= isLowFuel :
                lowFuel = true
            default :
                lowFuel = false
            }
        }
    }
}

```

Can’t you use switch in didSet?

Bob

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [June 15, 2017, 12:28pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/11 "2017-06-15T12:28:01Z")

</div>

The first line of [Apple documentation](https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/ControlFlow.html) about switch summarize it :

> A **switch** statement considers a value and compares it against several possible matching patterns.

The matching patterns must be of the same type of your value. So in your switch, you should use `Double` values for each case. Instead, you are using `Bool` :  
For example `level > max` results in a boolean, it’s true or false.

You can use boolean in a if…else control flow :

```
if level > max {
    level = max
} else if level < min {
    level = min
} else if level <= isLowFuel {
    lowFuel = true
} else {
    lowFuel = false
}

```

There is a switch -way to do so, but I consider it a little ugly :

```
switch level {
    case let x where x > max :
        level = max
    case let x where x < min :
        level = min
    case let x where x <= isLowFuel :
        lowFuel = true
    default:
        lowFuel = false
}

```

Totally unrelated to your question, but you should split your tests in two part, because if you initialise a FuelTank with a value lower than min, then you will not pass it the test which will set `lowFuel` to `true`.  
Also, `didSet` is not called during the initialization, so lowFuel will not be be set to true if you pass a value lower than `isLowFuel` as parameter.

Here is an example of functional `FuelTank` class :

```
struct FuelTank {

    private static let min = 0.0
    private static let max = 1.0
    private static let isLowFuel = 0.1

    var lowFuel: Bool = false
    var level: Double {
        didSet {
            testLevel()
        }
    }

    private mutating func testLevel() {
        if level < FuelTank.min {
            level = FuelTank.min
        } else if level > FuelTank.max {
            level = FuelTank.max
        }
        lowFuel = (level <= FuelTank.isLowFuel)
    }

    public init (level : Double){
    self.level = level
        testLevel()
    }
}  

```

Don’t hesitate to ask if you have further questions 🙂

---

<div class="post-metadata">

**Author:** ![sedawk](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/sedawk/32/81494_2.png) [@sedawk](https://forums.kodeco.com/u/sedawk)\
**Post date:** [June 16, 2017, 8:48pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/12 "2017-06-16T20:48:56Z")

</div>

Great Video.

Just a comment on the Ub3r H4ck3r Challenge solution: I believe having the students use a stored property for the lowFuel Boolean is a design mistake. Here is my solution:

```
struct FuelTank {
    var level : Double {
        didSet {
            if level > 1.0 {
                level = 1.0
            } else if level < 0.0 {
                level = 0.0
            }
        }
    }
    var lowFuel : Bool {
        return level < 0.10
    }
}

```

The solution file requires you to answer the “lowFuel” question when creating a tank, which makes no sense.

`var theTank = FuelTank(lowFuel: false, level: 1.0)`

Where as with my solution:

`var theTank = FuelTank(level: 1.0)`

To me a property like 'lowFuel" screams to be a computed property. Is there some design element that I am missing that should have lowFuel be a stored Property?

EDIT: I was thinking you might want to have the lowFuel actively set because you want to “turn-on” a low fuel indicator when this situation happens. However, you could still check the lowFuel computed property with the level didSet and the react if required.

---

<div class="post-metadata">

**Author:** ![micazeve](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/micazeve/32/103912_2.png) [@micazeve](https://forums.kodeco.com/u/micazeve)\
**Post date:** [June 17, 2017, 1:03pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/13 "2017-06-17T13:03:53Z")

</div>

You are right, a computed property could be used here since the value of ‘lowFuel’ is always dependent of the value of ‘level’, and thus cannot be set explicitly.

---

<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, 4:19pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/14 "2017-07-13T16:19:12Z")

</div>

@sedawk Thanks!! This made much more sense!

---

<div class="post-metadata">

**Author:** ![arvnq](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/arvnq/32/85416_2.png) [@arvnq](https://forums.kodeco.com/u/arvnq)\
**Post date:** [August 6, 2017, 12:25pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/15 "2017-08-06T12:25:42Z")

</div>

i’ve been thinking about the solution of the challenge and still can’t figure how to do it. When I checked the solution, i tried initializing a tank var but it seems to not solve the issue.

var tank = FuelTank(lowFuel: false, level: 0)  
print(“Fuel level: (tank.level)”) //will output 0.0  
print(“is fuel low? : (tank.lowFuel)”) //will output false

i believe @sedawk 's answer and @micazeve 's explanation is much more sensible …  
Hope this is changed for future version of this tutorial though. (Swift4 and up)

---

<div class="post-metadata">

**Author:** ![argyrus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/argyrus/32/81494_2.png) [@argyrus](https://forums.kodeco.com/u/argyrus)\
**Post date:** [August 13, 2017, 8:40am UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/16 "2017-08-13T08:40:08Z")

</div>

Hi All  
I am using the following code for the uber hacker challenge.

* * *

struct FuelTank {  
var lowFuel: Bool{  
return level \< 0.01  
}  
var level: Double {  
willSet {  
if newValue \> 1.0 {  
level = 1.0  
} else if level \< 0 {  
level = 0  
}  
}  
}  
}  
var tank1 = FuelTank(level: 12.0)  
print(tank1.lowFuel)  
print(tank1.level)

* * *

The outputs are false & 12.0, respectively.  
Shouldn’t it be false & 1.0?

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 13, 2017, 8:06pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/17 "2017-08-13T20:06:07Z")

</div>

You need to use a `didSet` property observer which runs just after the `level` variable’s value is set and change the tank’s fuel level value right after you initialize the `tank1` object with the `FuelTank` structure’s default initialiser like this:

```
struct FuelTank {
  var lowFuel: Bool{
    return level < 0.01
  }
  var level: Double {
    didSet {
      if oldValue > 1.0 {
        level = 1.0
      } else if level < 0 {
        level = 0
      }
    }
  }
}
var tank1 = FuelTank(level: 12.0)
tank1.level = 10.0
print(tank1.lowFuel) // false
print(tank1.level) // 1.0
```

Please let me know if you have any other questions or issues regarding the whole thing.

---

<div class="post-metadata">

**Author:** ![argyrus](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/argyrus/32/81494_2.png) [@argyrus](https://forums.kodeco.com/u/argyrus)\
**Post date:** [August 15, 2017, 12:12pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/18 "2017-08-15T12:12:19Z")

</div>

Thanks a lot.  
It worked.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 15, 2017, 12:51pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/19 "2017-08-15T12:51:10Z")

</div>

We are in the process of updating the whole video course for Swift 4 and Xcode 9 after all so this thread and topic is closed for further questions and issues regarding it for now until then.

---

<div class="post-metadata">

**Author:** ![shogunkaramazov](https://assets.chunter.kodeco.com/user_avatar/forums.kodeco.com/shogunkaramazov/32/108826_2.png) [@shogunkaramazov](https://forums.kodeco.com/u/shogunkaramazov)\
**Post date:** [August 15, 2017, 12:51pm UTC](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015/20 "2017-08-15T12:51:31Z")

</div>

Closed on August 15, 2017, 12:51pm UTC



[Next page](https://forums.kodeco.com/t/intermediate-swift-3-part-4-properties-ray-wenderlich/21015.md?page=2)
