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

The Starlord
The Starlord
886 Points

Instead of using self.factbook, cant we create an instance of factbook class and use the object to access its properties

We did self.factbook = [[factbook alloc]init], why dint we do

factbook * obj = [[factbook alloc]init], just like we normally create instances

5 Answers

Stefan Jansen
Stefan Jansen
8,974 Points

the advantage of using self.factbook is that you only have to instantiate it once and you can use it all over the controller.

The Starlord
The Starlord
886 Points

It is the same with any instance right ? If i were to create an instance of a class i can use that in the whole class.. correct ?

factbook * obj = [[factbook alloc]init // in this case i can use "obj" just like self.facbook ?

Stefan Jansen
Stefan Jansen
8,974 Points

It works for any object.

you can't use it like that. the factbook instance in obj will only be locally within the method (in that scope) you define it. That is why you want to have a property in your class.

to make things clear, here is an example of a viewcontroller:

#import "ViewController.h"

@interface MyViewController : ViewController

@property NSString *property;

@end
#import "MyViewController.h"

@interface MyViewController ()

@end

@implementation MyViewController
- (void)viewDidLoad {
    [super viewDidLoad];

    NSString *property = @"value1"; // only available in this scope
    self.property = @"value2"; // available within the viewcontroller

    NSLog(@"property: %@", property); // output: "property: value1"
    NSLog(@"self.property: %@", self.property); // output: "self.property: value2"
}

- (void)customMethod
{
    NSLog(@"property: %@", property); // generates undefined identifier 'property' error
    NSLog(@"self.property: %@", self.property); // output: "self.property: value2"
}
The Starlord
The Starlord
886 Points

Thank you and i appreciate your effort in explaining it.....instead of creating a new object in a method if I create it outside then the scope of the object is whole class right, isn't the below code valid ?

@implementation MyViewController

NSString * obj = @"value 1";

  • (void)viewDidLoad { [super viewDidLoad];

    self.property = @"value2"; // available within the viewcontroller

    NSLog(@"property: %@", obj); // output: "property: value1" NSLog(@"self.property: %@", self.property); // output: "self.property: value2" }

  • (void)customMethod { NSLog(@"property: %@", obj); // displays value 1 NSLog(@"self.property: %@", self.property); // output: "self.property: value2" }

Stefan Jansen
Stefan Jansen
8,974 Points

Yes your code is valid, but I would advice against this way of declaring "private" properties.

You can declare "private" properties in 2 ways, i've commented them in the example below.

// MyViewController.m

#import "MyViewController.h"

@interface MyViewController ()
// declare "private" properties here
@end

@implementation MyViewController
{
// or declare "private" properties here
NSString *_property;
}

- (void)viewDidLoad {
    [super viewDidLoad];

    _property = @"value1";

    NSLog(@"property: %@", _property); // output: "property: value1"
    NSLog(@"self.property: %@", self.property); // output: "self.property: value2"
}

- (void)customMethod
{
    NSLog(@"property: %@", _property); // output: "property: value1"
    NSLog(@"self.property: %@", self.property); // output: "self.property: value2"
}