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

  • Home
  • SEARCH
  • 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 6553143
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T12:32:47+00:00 2026-05-25T12:32:47+00:00

I have created a class, Metrics that is designed to be subclassed to customize

  • 0

I have created a class, Metrics that is designed to be subclassed to customize behavior. In order to make this more robust, the init method of Metrics calls a method named setup that does nothing by default. If subclasses want to customize behavior during initialization (which they commonly do) they can override this method. Since the default implementation does nothing, there’s no need to remember to call [super setup].

I like the way that this works so far, it’s robust and easy to use. The problem I have now is that there are times when the setup method requires some additional property to be set. An example:

@implimentation Metrics

- (id)initWithFrame:(CGRect)frame {
  self = [super initWithFrame:frame];
  if (self) {
    // Do all the required initialization
  }
}

@end

@interface SubclassOfMetrics : Metrics {}

@property (assign) CGFloat width;

@end

@implimentation SubclassOfMetrics

- (void)setup {
  // This method is indirectly called as a result of the superclass initialization
  // The code here depends on the `width` property being set
}

@end

The problem I have is that setup is called before the property width can be set. What options do I have here? I can work around this by not initializing my Metrics class in the init method. I can do it explicitly after I have set the properties I need to set. I’m not of fan of this, as it requires me to perform actions in a certain order and remember to setup. Do I have any other options?

Edit: The root of problem is really that the initialization of the Metrics class does a lot of calculations. The results of those calculations will depend on the properties being set in a subclass, so I need a way to get those properties set before the superclass is done initializing.

  • 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-05-25T12:32:47+00:00Added an answer on May 25, 2026 at 12:32 pm

    I set the iVars before initializing the superclass.

    - (id)initWithFrame:(CGRect)frame andMetricWidth:(CGFloat)inWidth {
      _width = inWidth
    
      self = [super initWithFrame:frame];
      if (self) {
      }
    
      return self;
    }
    

    I intentionally set the iVar directly to avoid any potential side-effects from using the property syntax. I haven’t fully thought through how this would work with objects under ARC, but it works just fine with primitives.

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

Sidebar

Related Questions

I have created a class that inherits from RadioButtonList in order to add a
I have created a class Person that looks like this: public class Person {
I have a collection of classes that inherit from an abstract class I created.
I have a control that is created like so: public partial class MYControl :
I have created a class called BasePage which inherits System.Web.UI.Page. On this page I've
i have created this class for mesh loading it works but i added this
I have created a class descending from TFrame. The class was designed visually in
I have created a class that inherits the QListWidget and is meant to be
I have created a class (ConflictingBooking) that contains fields for various information in regards
I have created a class called ShortestTime. I have some functions within it that

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.