Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

objective-c default init method for class?

I have two differing methods for initializing my objective-c class. One is the default, and one takes a configuration parameter. Now, I'm pretty green when it comes to objective-c, but I've implemented these methods and I'm wondering if there's a better (more correct/in good style) way to handle initialization than the way I have done it. Meaning, did I write these initialization functions in accordance with standards and good style? It just doesn't feel right to check for the existence of selfPtr and then return based on that.

Below are my class header and implementation files. Also, if you spot anything else that is wrong or evil, please let me know. I am a C++/Javascript developer who is learning objective-c as hobby and would appreciate any tips that you could offer.

#import <Cocoa/Cocoa.h>

// class for raising events and parsing returned directives

@interface awesome : NSObject {
 // silence is golden. Actually properties are golden. Hence this emptiness.
}

// properties
@property (retain) SBJsonParser* parser;
@property (retain) NSString* eventDomain;
@property (retain) NSString* appid

// constructors
-(id) init;
-(id) initWithAppId:(id) input;

// destructor
-(void) dealloc;


@end

#import "awesome.h"
#import "JSON.h"


@implementation awesome



- (id) init {
 if (self = [super init]) {
  // if init is called directly, just pass nil to AppId contructor variant
  id selfPtr = [self initWithAppId:nil];
 }

 if (selfPtr) {
  return selfPtr;
 } else {
  return self;
 }
}

- (id) initWithAppId:(id) input {
 if (self = [super init]) {
  if (input = nil) {
   input = [[NSString alloc] initWithString:@"a369x123"];
  }
  [self setAppid:input];
  [self setEventDomain:[[NSString alloc] initWithString:@"desktop"]];
 }
 return self;
}

// property synthesis
@synthesize parser;
@synthesize appid;
@synthesize eventDomain;

// destructor
- (void) dealloc {
 self.parser = nil;
 self.appid = nil;
 self.eventDomain = nil;
 [super dealloc];
}

@end

Thanks!

like image 379
Alex Avatar asked Dec 27 '10 23:12

Alex


People also ask

What is Init method in Objective-C?

Out of the box in Objective-C you can initialize an instance of a class by calling alloc and init on it. // Creating an instance of Party Party *party = [[Party alloc] init]; Alloc allocates memory for the instance, and init gives it's instance variables it's initial values.

How to initialize a class object C?

There are two ways to initialize a class object: Using a parenthesized expression list. The compiler calls the constructor of the class using this list as the constructor's argument list. Using a single initialization value and the = operator.

What is the importance of init() method in apple?

init() Implemented by subclasses to initialize a new object (the receiver) immediately after memory for it has been allocated.


2 Answers

When one initializer simply performs the more complex initializer with some default parameters, call it as such:

-(id)init {
  return [self initWithAppID:nil];
}

-(id)initWithAppID:(id)input {
  if (self = [super init]) {
    /* perform your post-initialization logic here */
  }
  return self;
}

Usually you try to make one of the initializers the "designated initializer", meaning it's the one that always gets invoked. In this case that's -initWithAppID:.

like image 55
d11wtq Avatar answered Sep 28 '22 07:09

d11wtq


Your init method should call the preferred initializer, initWithAppId:, instead of the super implementation. Then the initWithAppId calls the super implementation, as it does. Also, in initWithAppId:, you have if(input = nil), which will always set input to nil and evaluate to YES. Here are the proper implementations.

- (id)init {
    return [self initWithAppId:nil];
}
- (id)initWithAppId:(id)input {
    if((self = [super init])) {
        if(input == nil) input = @"a369x123";
        self.appid = input;
        self.eventDomain = @"desktop";
    }
    return self;
}
like image 21
ughoavgfhw Avatar answered Sep 28 '22 06:09

ughoavgfhw