Group Group Group Group Group Group Group Group Group

Programming in Swift · Properties | Ray Wenderlich

#1

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

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…

#3

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

Have you tried another browser/device?

Grts,
Cera

#4

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

#5

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.

#6

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

#7

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?

#8

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) }
}
#9

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.

#10

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

#11

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.