Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7752815
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 1, 20262026-06-01T11:53:44+00:00 2026-06-01T11:53:44+00:00

According to the documentation, a class’s designated initializer in Objective-C must call the designated

  • 0

According to the documentation, a class’s designated initializer in Objective-C must call the designated initializer of its base class.

Another rule is that secondary initializers must call the designated initializer of their own class.

But if the second rule is followed, why can’t a designated initializer call a secondary initializer in its base class? This base secondary initializer will ultimately call its own level’s D.I., so the object will still be properly initialized, right?

The difference seems to be who chooses defaults for missing variables — you or your base class.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-01T11:53:45+00:00Added an answer on June 1, 2026 at 11:53 am

    Let’s consider NSSet. It has a designated initializer:

    - (id)initWithObjects:(const id *)objects count:(NSUInteger)cnt {
       // initialization code here
       return self;
    }
    

    It also has some secondary initializers, like this one:

    - (id)initWithArray:(NSArray *)array {
        NSUInteger count = array.count;
        id objects[count];
        [array getObjects:objects range:NSMakeRange(0, count)];
        return [self initWithObjects:objects count:count];
    }
    

    Now you want a subclass of NSSet that automatically rejects the string “Bob”. So you dutifully override the designated initializer in your subclass, but you call one of super’s secondary initializers:

    @implementation BobRejectingSet
    
    - (id)initWithObjects:(const id *)objects count:(NSUInteger)count {
        NSMutableArray *array = [[NSMutableArray alloc] initWithCount:count];
        for (NSUInteger i = 0; i < count; ++i) {
            if (![objects[i] isEqual:@"Bob"]) {
                [array addObject:objects[i]];
            }
        }
        return [super initWithArray:array];
    }
    

    What happens when you do this:

    BobRejectingSet *noBobs = [[BobRejectingSet alloc] initWithArray:someObjects];
    

    Since you didn’t override initWithArray:, the program calls -[NSSet initWithArray:], which calls the designated initializer, initWithObjects:count:. You overrode the designated initializer, so it calls your method. Your method filters out the Bobs, and then calls super’s secondary initializer, initWithArray:… which turns around and calls your designated initializer override again. Unlimited recursion. Stack overflow. You get the segmentation-violation-core-dumped blues.

    That’s why you always use super’s designated initializer.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

According to the documentation of java.util.Pattern , the POSIX character class \p{Graph} ( [:graph:]
According to PHP documentation, HttpResponse is a static class to manipulate the Http response
According to the Ruby Set class's documentation, == Returns true if two sets are
According to my understanding of the Apple's documentation the 'CIFaceFeature' class' object is a
In the FileHandle Class there is a fileHandleWithStandardOutput method. According to the Documentation ,
The BitConverter class has a field IsLittleEndian which, according to the documentation: Indicates the
According to the documentation : There is no way to block signals temporarily from
According the Perl documentation on regexes : By default, the "^" character is guaranteed
According to MSDN Documentation for partial classes : Partial methods are implicitly private So
According to the documentation, there is no convenient 'same' option for the xcorr command

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.