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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T03:55:25+00:00 2026-05-20T03:55:25+00:00

If I have a class hierarchy in which subclasses require use of more specific

  • 0

If I have a class hierarchy in which subclasses require use of more specific types than those specified in the superclasses’ ivars, is it better to declare the superclass ivar as id, or to type it and then cast where necessary in the subclasses?

For example I have a superclass which uses an ivar of type Thing:

@interface SuperClass {
    Thing *_typedIvar; // OR 
    id anonIvar;
}
@property (nonatomic, retain, readwrite) Thing *_typedIvar; 
@property (nonatomic, retain, readwrite) id anonIvar; // OR

Then in a subclass implementation I want to use a SubThing to get at its methodNotInThing

@interface SubClass : SuperClass {
}
@implementation {

    - (void)aMethod {

       SubThing *subtypedIvar = (SubThing *)self.typedIvar;
       [subtypedIvar methodNotInThing];

       // OR

       [self.anonIvar methodNotInThing];


    }
}

(I’m assuming here that the subclass has appropriately assigned a SubThing to the ivar).

I’ve used both approaches (in my thus far short time using ObjC), and am never quite resolved regarding which is best. I like the compiler checking offered by use of real types, along with being able to use dot syntax where appropriate. But the constant casting in subclasses gets pretty ugly, requires more code, and somehow tastes worse to me (which is hardly an argument, I suppose). However I like the fact in the typed version that the superclass in effect documents what subclasses should do with the ivar.

Which of the above would be the better approach, and why?

  • 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-20T03:55:26+00:00Added an answer on May 20, 2026 at 3:55 am

    I would argue that using real types is better, despite the drawback of having your code littered with casts.

    • The programmer should be in control. Using id here is a bit careless.
    • Easier to follow, and therefore easier to debug.
    • While having casts everywhere may look messy, it forces you to be aware of what types you are expecting and again, this comes back to maintenance. It might work with id for right now, but if you have calls to id all over the place, how will you know why a particular one isn’t working?
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

No related questions found

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.