简体   繁体   中英

How and where self object is alloc?

There are many methods which override functions like this:

- (void)viewDidLoad
{
    [super viewDidLoad];
}

So the super is calling the parent classes function, but where is self allocated?

self is a pointer to the “current object”, it's allocated in the usual initialization formula:

NSObject *foo = [[NSObject alloc] init];

When you call [NSObject alloc] , the class creates an object that you later refer to as self .

Whenever we initialize our class like this with nib:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

or simply init the self will be allocated

Basically it is initialized in init function like this:

- (id) init
{
  self = [super init];
  return self;
}

Here

self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];  

This is assigning value in self

When you call you class and alloc it then it access it's super class init method and pass to self.

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self)
    {

    }
    return self;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM