Welcome to the Treehouse Community

The Treehouse Community is a meeting place for developers, designers, and programmers of all backgrounds and skill levels to get support. Collaborate here on code errors or bugs that you need feedback on, or asking for an extra set of eyes on your latest project. Join thousands of Treehouse students and alumni in the community today. (Note: Only Treehouse students can comment or ask questions, but non-students are welcome to browse our conversations.)

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and a supportive community. Start your free trial today.

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";
}