"Angular Basics" was retired on April 12, 2021.

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

Difference between an instance variable and property

I have a belief that properties were meant to replace instance variables, however I have seen code where developers use both together. Would someone detail when or why one is preferred over the other?

3 Answers

Thank you, Amit! I guess to clarify my question is: are instance variables declared in the implementation more secure, or are they faster? From what I gather after watching the video is that properties lessen the need to declare instance variables. However when programmers use them both, it does not make sense. They could just use properties exclusively.

Amit Bijlani
Amit Bijlani
Treehouse Guest Teacher

It's not that a property lessens the need for instance variables. When you declare a property an instance variable along with getter/setter methods are created for you automatically. So there is an instance variable which is usually prefixed with an underscore. For example, you created a property named titles then there is an instance variable created for you named _titles. Think of property a shortcut way of defining instance variables that need getter/setter methods to be accessed. The only other reason you would not want to use properties would be to use an instance variable that is only used within the class and not outside of it.

Thanks Amit that helped very much!