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

Alex Watts
Alex Watts
8,396 Points

Unused variable.

Hello,

I have just started learning objective-c and was wondering why I got the message 'Unused variable' after I had written the following code:

int currentAge = 18;

After watching the tutorial at treehouse, Gabe Nadel's code looked liked this...

int currentAge;
currentAge = 18;

It seemed to work without displaying a message. Why is this?

2 Answers

Jennifer Nordell
seal-mask
STAFF
.a{fill-rule:evenodd;}techdegree
Jennifer Nordell
Treehouse Teacher

In your code you both declare the variable and initialize it. However, the code they use declares the variable without setting a value. And then later they "use" the variable to assign a value. Now, how useful that code is is up for debate :) But the point is, the compiler tries to implement safeguards to keep you from declaring variables that never reappear in your code. For example if I have three variables x, y, and z. If I start writing my code then determine I don't actually NEED variable z, but forget to remove the declaration, the compiler will warn me about this. Hope that makes sense!

Alex Watts
Alex Watts
8,396 Points

Thanks Jennifer this was very helpful!