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![](https://ecs-static.teamtreehouse.com/assets/views/marketing/shared/community-banner-white-47072046c51352fe6a69f5e691ff5700b28bb11d45197d7bdf066d9ea3f72d0c.webp)
![The Starlord](https://secure.gravatar.com/avatar/44b10a0e2c2dd397fc28d8a433294216?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
The Starlord
886 PointsInstead 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](https://secure.gravatar.com/avatar/ad22fe95cdfc30810a99abc8647330f6?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Stefan Jansen
8,974 Pointsthe 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](https://secure.gravatar.com/avatar/44b10a0e2c2dd397fc28d8a433294216?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
The Starlord
886 PointsIt 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](https://secure.gravatar.com/avatar/ad22fe95cdfc30810a99abc8647330f6?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Stefan Jansen
8,974 PointsIt 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](https://secure.gravatar.com/avatar/44b10a0e2c2dd397fc28d8a433294216?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
The Starlord
886 PointsThank 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](https://secure.gravatar.com/avatar/ad22fe95cdfc30810a99abc8647330f6?s=96&d=https%3A%2F%2Fecs-static.teamtreehouse.com%2Fassets%2Fcontent%2Fdefault_avatar-445fbbabfc8dc9188fb5967fe43322ee0c3e0dd1e10f378bf8343784af5a13eb.webp&r=pg)
Stefan Jansen
8,974 PointsYes 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"
}