Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS Object-Oriented Swift Properties Optional Property

Changing the value of a constant

We learned in past lessons that a value of object instantiated with "let" keyword can't be changed. It would be a constant.

But in this lesson, we initialised toy with "let" and after, we changed the value of "batteries" property.

Why can we change the value of the toy's property if it is instantiated with "let'?

let toy = Electronic(title: "RC Car", price: 79) 
toy.batteries = true

3 Answers

Holger Liesegang
Holger Liesegang
50,595 Points

Hi Leonardo!

You are right, one can't change the value of a constant but while you can't assign another object to the constant toy you can still make use of all the properties of this (constantly assigned) object :)

Liam Herbert
Liam Herbert
15,140 Points

Is there a question your are hoping to have answered here??? I do not understand what it is you are trying to ask.

Why can we change the value of the toy's property if it is instantiated with "let'?

Thanks, Holger. I hadn't realised that the fact that an object is constant does not automatically apply to its properties.