Programming in Swift · Properties | Ray Wenderlich


This is a companion discussion topic for the original entry at https://www.raywenderlich.com/5994-programming-in-swift/lessons/41

The video is stuck at the 8:40 mark? I tried reloading the webpage many times but can’t get the video to continue playing…

It’s working over here mate. ( past 8:40 )
@renderingpixels

Have you tried another browser/device?

Grts,
Cera

@renderingpixels Please let me know if you still have this issue when you get a chance. Thank you!

I’m just going through this course now, and the video is not getting hung up. (Chrome v71).
I did however notice something else about this lesson, and a small issue with the supplied starter Playground.

The part where we call the ‘summonMagicalCreature’ function did not initially work for me. The problem is that the summonMagicalCreature.swift file is located in the Sources folder for the ‘Challenge - Properties’ page and not the ‘Properties’ page. I created a new file in the Sources folder of the ‘Properties’ page and copied and pasted the summonMagicalCreature code to get it to work.

Thanks so much for letting us know! I updated the starter playground for this part and put summonMagicalCreature in the right place. :]

I’m trying to understand better how set works. When exactly newValue is assigned to property? It is not explicitly done in the code. Is it when the closure runs till the end without calling return?

set is the “assignment”. A computed property with both get and set is functionally the same (it’s just cleaner syntax) as having a set of methods, like this:

func getFullName() -> String {
  return "\(firstName) \(lastName)"
}

mutating func setFullName(_ newValue: String) {
  let nameSubstrings = newValue.split(separator: " ")

  guard nameSubstrings.count >= 2 else {
    print("\(newValue) is not a full name.")
    return
  }

  let nameStrings = nameSubstrings.map(String.init)
  firstName = nameStrings.first!
  lastName = nameStrings.last!
}

var fullName: String {
  get { return getFullName() }
  set { setFullName(newValue) }
}

Hello,

To me, it is quite confusing why they named ‘get’ and ‘set’.

Please correct me if I am wrong.

When you use ‘get,’ is it correct to think in a perspective of a variable receiving a newly computed value?

For instance,

struct Age {

var bornYear : Int
var currYear : Int
var age : Int {
    get {return currYear - bornYear
        
    }
    
    set(newAge) {
        bornYear = currYear - newAge
    }
}

}

This case, age is receiving a newly computed value, which is currYear minus bornYear. Therefore, age is 'get’ting this new value.

On the other hand, if I want to assign a new value to the age variable, i need to implement ‘set’ to set the new value, in this case ‘newAge.’ Later, I can compute new ‘bornYear’ and ‘currYear’ based off a newly set ‘newAge.’

Am I understanding Get and Set correctly??

Thanks.

Try using your struct to see what get and set do:

struct Age {
  var bornYear : Int
  var currYear : Int
  var age : Int {
    get { return currYear - bornYear }
    set(newAge) { bornYear = currYear - newAge }
  }
}

var test = Age(bornYear: 2001, currYear: 2019)

// this line gets the age and bornYear properties from test
print ("age: \(test.age); born: \(test.bornYear)")

// this line sets the age property of test
test.age = 14

// this line gets the age and bornYear properties from test again
print ("age: \(test.age); born: \(test.bornYear)")

results:
age: 18; born: 2001

age: 14; born: 2005

The part where you say timing difference between lazy and computed property is not clear. Can you point explain? Why is the lazy variable kind of storing the old value of a computed property?

Did you mean that…
First the lazy property is calculated. so we use severus
Then after that method call computed property will be calculated.

@jessycatterwaul @catie Can you please help with this when you get a chance? Thank you - much appreciated! :]

Hi!

Unlike a computed property, a lazy variable’s value is only evaluated the first time you use the lazy variable. After that point, the value that was evaluated is stored, and will be the same forever. …Unless you manually assign to the variable again! You have the freedom to do that as many times as you want.