Note that there are some explanatory texts on larger screens.

plurals
  1. PODesignated initializer and convenience initializer in objective-c and how to properly create them and tie them together
    text
    copied!<p>I am trying to understand which should be designated initializer and which one convenience initializer. I read apple docs on that topic but i am still not sure. Does the designated initializer has to have all the values that requires for the class? For example<br> This was the first designated initializer that i created</p> <pre><code>-(id)initWithCardCount:(NSUInteger)cardCount usingDeck:(Deck *)deck { self = [super init]; if (self) { for(int i = 0; i&lt;=cardCount;i++){ Card *card = [deck drawRandomCard]; if (!card) { self = nil; break; }else{ self.cards[i] = card; } } } return self; } </code></pre> <p>Now adde <code>cardMatchMode</code> property to this class and would like to set it in the initializer. To make the class backward compatible and to understand initializers i am keeping the one that i have now and creating another initializer. </p> <pre><code>-(id)initwithCardCount:(NSUInteger)cardCount usingDeck:(Deck *)deck cardMatchMode:(NSUInteger)matchMode { _cardMatchMode = matchMode; return [self initWithCardCount:cardCount usingDeck:deck];; } </code></pre> <p>Based on the apple docs the convenience initializer has to return the value for the designated initializer but the question is can i set the extra property on this class in the convenience initializer? Can i say <code>self.cardMatchMode = matchMode;</code> or self is still not fully initialized?<br> It does work but i just wanted to understand if this is proper code and i can access the cardMatchMode property in the convenience init or i would have to make <code>-(id)initwithCardCount:(NSUInteger)cardCount usingDeck:(Deck *)deck cardMatchMode:(NSUInteger)matchMode</code> </p> <p>as the designated initializer and the other one convenience init and rework the code? Thanks!</p> <p>/////Update</p> <p>I was getting an error in the </p> <pre><code>-(id)initwithCardCount:(NSUInteger)cardCount usingDeck:(Deck *)deck cardMatchMode:(NSUInteger)matchMode </code></pre> <p>When i tried to do <code>self = [self initWithCardCount:(NSUInteger)cardCount usingDeck:(Deck*)deck;</code> the error said that you cannot assign self outside the init family. I figured it out what was the problem. The init method had lowercase w and it had to be capital so now it works. This is the code i have now for my convenience initializer.</p> <pre><code>-(id)initWithCardCount:(NSUInteger)cardCount usingDeck:(Deck *)deck cardMatchMode:(NSUInteger)matchMode { self = [self initWithCardCount:cardCount usingDeck:deck]; if (self){ _cardMatchMode = matchMode; } return self; } </code></pre> <p>Now that makes more sense. I called the designated init which calls super and then i set the <code>cardMatchMode</code> variable. </p> <p>As far as i understand there are a lot of objects that have convenience initializer with extra parameters and it will just call's designated init. If you look at NSString and it has different initializers with different parameters. It's probably calling init which is the designated initializer. Is this correct?</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload