Hi,
Can someone explain why the use of the "self" keyword when lazy instantiating?
Here I'm lazy instantiating a class called ModelClass, which requires to use of the "self" keyword when calling methods, why?
Here I'm directly instantiating class called ModelClass and does NOT require the "self" keyword.
Can someone explain the use of the "self" keyword in general?
Thanks a lot for your help!
Can someone explain why the use of the "self" keyword when lazy instantiating?
Here I'm lazy instantiating a class called ModelClass, which requires to use of the "self" keyword when calling methods, why?
Code:
#import "ViewController.h"
#import "ModelClass.h"
@interface ViewController ()
@property (nonatomic, strong) ModelClass *model;
@end
@implementation ViewController
@synthesize model = _model;
-(ModelClass *) model
{
if (!_model)
{
_model = [[ModelClass alloc]init];
}
return _model;
}
- (IBAction)showMessage:(id)sender
{
NSString *word = @"This is a message";
self.display.text = [self.model message:word];
}
@end
Here I'm directly instantiating class called ModelClass and does NOT require the "self" keyword.
Code:
#import "ViewController.h"
#import "ModelClass.h"
@interface ViewController ()
@end
@implementation ViewController
- (IBAction)showMessage:(id)sender
{
NSString *word = @"This is a message";
ModelClass *model = [[ModelClass alloc]init];
self.display.text = [model message:word];
}
@end
Can someone explain the use of the "self" keyword in general?
Thanks a lot for your help!