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 Implementing Designs for iPhone Customizing Table View Controllers Customizing Table View Cell Checkmarks

Kerem Donmez
Kerem Donmez
7,149 Points

Variable declaration before viewDidLoad

What happens if I declare the value of a variable before viewDidLoad and use it somewhere in that class? İt might be a stupid question but I currently don't have a mac book and just can't try it out so if someone can tell me I would appreciate it.

1 Answer

Stone Preston
Stone Preston
42,016 Points

you can do that. that would be whats known as an instance variable or ivar:

@implementation MyClass {

    // declare it here
    NSString *myVariable;
}

// methods go here
// you can access myVariable within the class implementation
@end
Kerem Donmez
Kerem Donmez
7,149 Points

Thanks for the reply, but it seems I forgot some stuff in my question.I know instance variables , I was trying to ask what would happen if you also set its initial value there before where It is usually set, in viewDidLoad. Then try to access it without touching it in viewDidLoad.

Stone Preston
Stone Preston
42,016 Points

you can do that

@implementation MyClass {

    // declare it and assign it a value
    NSString *myVariable = @"some string";
}