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 8681705
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T21:34:47+00:00 2026-06-12T21:34:47+00:00

I am coding in Objective-C / Cocoa-Touch for iPhone, and I was wondering, is

  • 0

I am coding in Objective-C / Cocoa-Touch for iPhone, and I was wondering, is it ever OK to not chain initializers? For instance, in my parent class I have three initializers that all need to be inherited from by child class. Is it OK in this instance to not chain? For instance, in this one class I have it set up like this:

- (id)initWithGraphic:(Graphic*)graphic size:(CGSize)size
{
    self = [super initWithFrame:CGRectZero];
    if (self) 
    {
      ...

    }
    return self;
}

- (id)initWithGraphic:(Graphic*)graphic maximumDimension:(CGFloat)maximumDimension
{
    self = [super initWithFrame:CGRectZero];
    if (self) 
    {
      ...

    }
    return self;
}

- (id)initWithGraphic:(Graphic*)graphic 
{
    self = [super initWithFrame:CGRectZero];
    if (self) 
    {
      ...

    }
    return self;
}

So essentially I don’t have one designated initializer because it’s tough for me to chain them together, I basically have three.

  • 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-12T21:34:49+00:00Added an answer on June 12, 2026 at 9:34 pm

    Short answer: if you want your code to be understandable and maintainable, then you must always call super‘s designated initializer.

    Long answer…

    The -[NSObject init] method is documented to do nothing:

    The init method defined in the NSObject class does no initialization; it simply returns self.

    Therefore in theory you’re not required to call it. In practice it’s better to call it because it makes your code more uniform (and therefore easier to understand), and less likely to break if you decide to change a class to inherit from something other than NSObject.

    If you have some inheritance chain MyGrandparent > MyParent > MyObject, and you are the implementer of both MyParent and MyObject, and MyParent doesn’t override all of MyGrandparent‘s initializers, then you could directly call one of MyGrandparent‘s non-overridden initializers from your MyObject initializers. But again, this is a bad idea. It will be confusing when you or someone else has to revisit the code later, and likely to break if you change the implementation of MyParent later.

    Also, if you’re creating multiple initializers, you need to learn about designated initializers here and here. It’s important to pick one of your initializers as the designated initializer, and make sure all subclass initializers call the superclass’s designated initializer directly. Otherwise, you’re likely to end up creating infinite recursion. I explained this problem thoroughly in this answer and this answer.

    UPDATE

    In your example, you could set up a designated initializer like this:

    static CGFloat const kMaximumDimensionNone = 0; // or NAN if 0 is a valid max dimension
    static CGSize const kSizeNone = { 0, 0 }; // or NAN, NAN if 0x0 is a valid size
    
    - (id)initWithGraphic:(Graphic *)graphic size:(CGSize)size maximumDimension:(CGFloat)maximumDimension {
        if (self = [super init]) {
            _graphic = graphic;
            if (!CGSizeEqualToSize(size, kSizeNone)) {
                [self configureWithSize:size];
            }
            else if (maximumDimension != kMaximumDimensionNone) {
                // Use if(!isnan(maximumDimension)) instead if you use NAN as the placeholder
                [self configureWithMaximumDimension:maximumDimension];
            }
            else {
                [self configureWithDefaultDimensions];
            }
        }
        return self;
    }
    
    - (id)initWithGraphic:(Graphic *)graphic {
        return [self initWithGraphic:graphic size:kSizeNone maximumDimension:kMaximumDimensionNone];
    }
    
    - (id)initWithGraphic:(Graphic *)graphic size:(CGSize)size {
        return [self initWithGraphic:graphic size:size maximumDimension:kMaximumDimensionNone];
    }
    
    - (id)initWithGraphic:(Graphic *)graphic maximumDimension:(CGFloat)maximumDimension {
        return [self initWithGraphic:graphic size:kSizeNone maximumDimension:kMaximumDimensionNone];
    }
    

    You don’t have to expose the designated initializer in the public header file. You can add a a separate header file exposing the designated initializer for subclasses to import. For example, Apple does this with the UIGestureRecognizerSubclass.h header, which declares the ForSubclassEyesOnly category on UIGestureRecognizer.

    Or you could expose the configureWith... methods to your subclasses, and maybe you wouldn’t even need to have them override the initializers.

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

Sidebar

Related Questions

Are there any generally-accepted coding standards (naming, casting etc) that apply specifically to iPhone/Cocoa/Objective-C?
Google Coding Standard for Objective C says we should not throw exceptions , what
I am new to the objective c coding in iPhone. i am using below
I am coding in the latest xcode in Objective-C and I have been pretty
I'm not very experienced with Cocoa but I am with MVC and coding in
I have a general coding style question for Objective C. When I have a
I have been struggling to understand the difference between Storyboards and coding in Objective-C.
I'm new to Xcode and Objective C coding, and I already have a little
I'm coding in C++ using CoreAnimation. In Objective-C the CALayer instance is being created
Is there any pdf which tells about coding guidelines in objective C. For Example...

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.