Objective-c default init method for class?

I have two different methods for initializing my objective-c class. One is the default, and one accepts a configuration parameter. Now I'm pretty green when it comes to objective-c, but I have implemented these methods, and I am wondering if there is a way (more correct / good style) to handle initialization than how I did it, Sense, I wrote these functions initialization to standards and good style? It’s just not worth checking the existence of selfPtr , and then returning to that.

The following are header files and class files. Also, if you notice anything else that is wrong or evil, let me know. I am a C ++ / Javascript developer who studies objective-c as a hobby and will be grateful for any advice 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!

+7
source share
4 answers

When a single initializer simply executes a more complex initializer with some default parameters, name 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 a “designated initializer”, which means that it is always called. In this case, -initWithAppID:

+14
source

Your init method should call the preferred initializer, initWithAppId :, instead of super-executing. Then initWithAppId calls the super implementation, just like it does. Also, in initWithAppId :, you have an if (input = nil), which will always enter the value nil and evaluate YES. Here are the correct 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; } 
+1
source

Honestly, I see this as a moot point. Your second initialization method does not make sense when receiving the nil argument (plus you have a logical problem in your conditional check if the input is zero). In this case, I would like to provide one initialization method and two factory class methods that operate in the usual way: Return the auto-implemented instances and in one of them specify your default value.

For example, declare a class method:

 + (awesome*)awesome; + (awesome*)awesomeWithAppId:(id)foo; 

and in your implementation for +awesome , for example, write it like this:

 + (awesome*)awesome { return [[[awesome alloc] initWithAppId:@"a369x123"] autorelease]; } 

And similarly, in your awesomeWithAppId: something like this:

 + (awesome*)awesomeWithAppId:(id)foo { return [[[awesome alloc] initWithAppId:foo] autorelease]; } 

And again, it can only be me.

0
source

default will be selected by any of them,

 [awesome alloc] init]; [awesome alloc] initWithAppId:ID]; 
0
source

All Articles