Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Constructor in Objective-C

Tags:

objective-c

I was trying this sample program below.

I'm not calling the +(void)initialise and -(id)init method in the class B.But its getting called automatically.

Is the -(void)initialise is equal to the default constructor in objective C.

Does the [super init] points to the NSObject.

If i'm not using the -(id)init method i'm getting a warning that the class is with incomplete implementation.

ClassA.h

#import <Foundation/Foundation.h>  static int ab;  @interface ClassA : NSObject {     int a; }  + (void) initialize; - (id) init; - (void) displayNumOfInstance; - (void) disp;  @end 

ClassA.m

#import "ClassA.h"  @implementation ClassA  + (void) initialize {     ab=0; }  - (id) init {     self = [super init];     if (self!=nil) {         ab++;     }     return self; }  - (void) displayNumOfInstance {     NSLog(@"Number of instances of this class:%d",ab); }  - (void) disp {     NSLog(@"The value is %d",ab); }  @end 

ClassB.h

#import <Foundation/Foundation.h> #import "ClassA.h"  @interface ClassB : ClassA {  }  - (void) display;  @end 

ClassB.m

#import "ClassB.h"  @implementation ClassB  - (void) display {     ab=20;     NSLog(@"The value ab is %d",ab); }  @end 

class2.m

#import <Foundation/Foundation.h> #import "ClassA.h"  int main (int argc, const char * argv[]) {     ClassA *a = [[ClassA alloc]init];     [a disp];     [a release];      ClassB *b = [[ClassB alloc]init];     [b display];     [b release];      ClassA *a1 = [[ClassA alloc]init];     [a1 disp];     [a1 release];      ClassB *b1 = [[ClassB alloc]init];     [b1 display];     [b1 release];      return 0; } 

Output:

2011-04-30 15:31:42.490 class2[1674:a0f] 1 2011-04-30 15:31:42.493 class2[1674:a0f] The value ab is 20 2011-04-30 15:31:42.494 class2[1674:a0f] 2 2011-04-30 15:31:42.495 class2[1674:a0f] The value ab is 20 
like image 293
Angus Avatar asked Apr 30 '11 11:04

Angus


People also ask

Does Objective C have constructors and destructors?

In Objective-C you would do so in the init method even though you create a convenience constructor. The convenience constructor as the name suggests is a shortcut so you don't have to write out two statements namely: alloc and init.

What is constructor in C?

A constructor is a special type of member function that is called automatically when an object is created. In C++, a constructor has the same name as that of the class and it does not have a return type. For example, class Wall { public: // create a constructor Wall() { // code } };

How do I initialize a class in Objective C?

Test example = [[Test alloc] init]; example.name = @"s"; you can write something like this: Test example = [[Test alloc] initWithName:@"s"]; Please note that this is very common for initialization method to return newly created object, hence the initialization method usually returns 'id' type (not void).

What is constructor in IOS?

Constructor/initializer is a method which we call while creating a new object in Object Oriented Programming. In this method we write all the code related to initial setup of an object. Objective C have a default construction called init which is present in its root class NSObject. We can override default initializer.


1 Answers

The default construction usually start with has the following format -init or any variant upon this, e.g. -initWithFrame:.

The method +initialize is a class method (static method) that's called at least once when your application starts. You can use this method to initialize static variables that are useful across all instances of the class. This method might be useful to e.g. initialize a shared cache or a shared lookup map for a class.

For NSObject the -init method is the designated initializer, but for other classes this might differ. Apple documents the designated initializer in it's class headers using the NS_DESIGNATED_INITIALIZER macro. For example UIView subclasses should override -initWithFrame: and -initWithCoder: instead, as these methods are marked as designated initializer.

When subclassing and implementing a custom designated initializer, don't forget to initialize the super class as well. Let's for example have a UIView subclass that has a custom designated initializer -initWithFrame:title:. We would implement it as follows:

// A custom designated initializer for an UIView subclass.  - (id)initWithFrame:(CGRect)frame title:(NSString *)title  {     // Initialize the superclass first.      //     // Make sure initialization was successful by making sure      // an instance was returned. If initialization fails, e.g.      // because we run out of memory, the returned value would     // be nil.     self = [super initWithFrame:frame];     if (self)      {         // Superclass successfully initialized.         self.titleLabel.text = title     }     return self; }  // Override the designated initializer from the superclass to  // make sure the new designated initializer from this class is  // used instead. - (id)initWithFrame:(CGRect)frame {     return [[self alloc] initWithFrame:frame title:@"Untitled"]; } 

More details on initialising can be found on the Apple Developer website:

  • Object Initialization

  • Multiple Initializers

like image 166
Wolfgang Schreurs Avatar answered Oct 08 '22 14:10

Wolfgang Schreurs