Constructor in Objective-C
Yes, there is an initializer. It's called -init
, and it goes a little something like this:
- (id) init {
self = [super init];
if (self != nil) {
// initializations go here.
}
return self;
}
Edit: Don't forget -dealloc
, tho'.
- (void)dealloc {
// release owned objects here
[super dealloc]; // pretty important.
}
As a side note, using native language in code is generally a bad move, you usually want to stick to English, particularly when asking for help online and the like.
/****************************************************************/
- (id) init
{
self = [super init];
if (self) {
// All initializations you need
}
return self;
}
/******************** Another Constructor ********************************************/
- (id) initWithName: (NSString*) Name
{
self = [super init];
if (self) {
// All initializations, for example:
_Name = Name;
}
return self;
}
/*************************** Another Constructor *************************************/
- (id) initWithName:(NSString*) Name AndAge: (int) Age
{
self = [super init];
if (self) {
// All initializations, for example:
_Name = Name;
_Age = Age;
}
return self;
}