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 Build a Playlist Browser with Objective-C Building a Music Library Model Creating a Music Library

Shaun Kelly
Shaun Kelly
5,648 Points

Understanding constants?

Can anybody explain what the definition of constants are and provide any kind of further documentation about it ? From my understand it's a bit like property's..

1 Answer

Its quite simple to its precisely that "constant" - i.e something that is a constant won't change. If you open a playground, create a constant (let) = "Hello" and then try and append to the string and you will get an error.

Where constants are very useful is when you are declaring/initialising a class or structure. If you think about it when you create youre instance of said class/structure, you don't want it necessarily to be mutated later on in the program. As a simple example (very very simple);

var myClass = "Hello Class"
  let myclass = MyClass();
  myclass.doSomething(withSomeProperty);
  myclass = "New Class"

Say you have a VARIABLE which you put the 'name' of the class you are accessing in and the class itself in a similar name as well (youll notice one is capitalised one is not). If at some point you tried to change myclass instead of the string myClass the compiler would throw an error. However, you are still able to access instance methods of the class and/or structure.