0

I intend to call default init method in init method with arguments, in iOS. like this:

-(id)init{
    self = [super init];
    if (self) {
        Office =          [[NSString alloc]init];
    }
    return self;
}

-(id)initWithOffice:(NSString*)office{
    self = [self init];
    if (self) {
        self.Office = itemDescription;
    }
    return self;
}

My question is it a good practice, What should be done? I appreciate your response in advance,

pnuts
  • 58,317
  • 11
  • 87
  • 139
Zain Syed
  • 44
  • 4

3 Answers3

5

That will work, but I would prefer the following as it doesn't allocate an empty string, only to be replaced with the initializing string:

-(id)initWithOffice:(NSString*)office{
    self = [super init];    // Not [self init]
    if (self) {
        Office = office;    // OK if using ARC
    }
    return self;
}

The first init method doesn't make a great deal of sense; I think simply leaving Office as nil is better (NSString objects are immutable). As pointed out by @H2CO3, the initWithOffice method becomes the designated initializer for the class and all other init methods should use it to initialize the object. With that in mind the first init method should be:

-(id)init{
    return [self initWithOffice:nil];
}
trojanfoe
  • 120,358
  • 21
  • 212
  • 242
  • 1
    @trojanfoe I think you should mention that OP got the logic the other way around. Generally `init` is who should call the designated initializer with some meaningful default arguments. –  Jun 12 '13 at 12:33
  • @Trojanfoe NSString objects are mutable if we make them mutable. My point here is to ask, whether calling init method within another init method is good. it makes sense to me, because we give memory in init and use it in initWithOffice. – Zain Syed Jun 12 '13 at 12:34
  • +1 and a small addition: `Office` should be better named `office` (following the conventions) and provided that you auto-synthesized -> `_office = office;` – Alladinian Jun 12 '13 at 12:38
  • @Alladinian True, but I think that's a different question. – trojanfoe Jun 12 '13 at 12:39
  • Well I meant an addition for the benefit of the OP of course, nothing to do with the answer itself hence the +1 :) – Alladinian Jun 12 '13 at 12:42
1

Creating a method starts with initWith is to see what values will be passed. It helps you to remind which values should be sent and allocated in the method. Consider you have 4 variables to init when the view is initialized. It's best to keep a separate initWith method where you can init your view and other variables you customize.

Engnyl
  • 1,380
  • 16
  • 24
  • I intend to see if calling one initializer within another (default this case) is a good practice ? – Zain Syed Jun 12 '13 at 12:43
  • In your sample the code should be like this if you are going to use both the default and customized inits; -(id)init{ self = [super init]; if (self) { // } return self; } -(id)initWithOffice:(NSString*)office{ self.Office = itemDescription; } – Engnyl Jun 12 '13 at 12:47
  • Can you describe the difference? to me init method is suppose to acquire memory. – Zain Syed Jun 12 '13 at 12:53
  • 1
    check here http://stackoverflow.com/questions/4542628/objective-c-default-init-method-for-class – Engnyl Jun 12 '13 at 12:59
0

I think you should improve you object assignning logic,Like that...

-(id)initWithOffice:(NSString*)office{
    self = [self init];
    if (self) {
        self.Office =  [[NSString alloc] initWithString:office]; //Purpose is that //the office object can only be released by the self, non other classes (The owner //of the variable should be self).
    }
    return self;
}
Prateek Prem
  • 1,544
  • 11
  • 14