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

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

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

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 ?

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

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

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