objective-c default init method for class?

Posted by Alex on Stack Overflow See other posts from Stack Overflow or by Alex
Published on 2010-12-27T23:33:59Z Indexed on 2010/12/27 23:54 UTC
Read the original article Hit count: 396

Filed under:
|
|

Hello,

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!

© Stack Overflow or respective owner

Related posts about objective-c

Related posts about osx