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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T04:43:13+00:00 2026-05-15T04:43:13+00:00

I’m going to create a base class that implements very similar functions for all

  • 0

I’m going to create a base class that implements very similar functions for all of the subclasses. This was answered in a different question. But what I need to know now is if/how I can cast various functions (in the base class) to return the subclass object. This is both for a given function but also a function call in it.

(I’m working with CoreData by the way)

As a function within the base class (this is from a class that is going to become my subclass)

+(Structure *)fetchStructureByID:(NSNumber *)structureID inContext:(NSManagedObjectContext *)managedObjectContext {...}  

And as a function call within a given function:

Structure *newStructure = [Structure fetchStructureByID:[currentDictionary objectForKey:@"myId"]];
                                              inContext:managedObjectContext];

Structure is one of my subclasses, so I need to rewrite both of these so that they are “generic” and can be applied to other subclasses (whoever is calling the function).

How do I do that?

Update: I just realized that in the second part there are actually two issues. You can’t change [Structure fetch…] to [self fetch…] because it is a class method, not an instance method. How do I get around that too?

  • 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-15T04:43:14+00:00Added an answer on May 15, 2026 at 4:43 am

    If I understand your question correctly I believe the key is the [self class] idiom.

    As far as your update goes requesting a way to call a class method on the current class you can use [self class]. As in:

    Structure *newStructure = [[self class] fetchStructureByID:[currentDictionary 
                                                  objectForKey:@"myId"]];
                                                     inContext:managedObjectContext];
    

    EDIT: I redid this to return id per @rpetrich’s comment — much cleaner and avoids the need for -isKindOfClass: as long as you’re sure of the type of the instance you’re calling -createConfiguredObject on.

    As for the first part, you could just return an id (pointer to any object) and document that it will return an instance of the same class it’s called upon. Then in the code you need to use [self class] anywhere you instantiate a new object in a method.

    e.g. if you have a -createConfiguredObject method which returns an instance of the same class it’s called on, it would be implemented as follows:

    // Returns an instance of the same class as the instance it was called on.
    // This is true even if the method was declared in a base class.
    -(id) createConfiguredObject {
        Structure *newObject = [[[self class] alloc] init];
        // When this method is called on a subclass newObject is actually
        // an instance of that subclass
        // Configure newObject
        return newObject;
    }
    

    You can then use this in code as follows:

    StructureSubclass *subclass = [[[StructureSubclass alloc] init] autorelease];
    subclass.name = @"subclass";
    
    // No need to cast or use isKindOfClass: here because returned object is of type id
    // and documented to return instance of the same type.
    StructureSubclass *configuredSubclass = [[subclass createConfiguredObject] autorelease];
    configuredSubclass.name = @"configuredSubclass";
    

    For reference, what I was referring to with -isKindOfClass: and casting to the proper subclass is as follows:

    Structure *structure;
    // Do stuff
    // I believe structure is now pointing to an object of type StructureSubclass
    // and I want to call a method only present on StructureSubclass.
    if ([structure isKindOfClass:[StrucutreSubclass class]]) {
        // It is indeed of type StructureSubclass (or a subclass of same)
        // so cast the pointer to StructureSubclass *
        StructureSubclass *subclass = (StructureSubclass *)structure;
        // the name property is only available on StructureSubclass.
        subclass.name = @"myname";
    } else {
        NSLog(@"structure was not an instance of StructureSubclass when it was expected it would be.");
        // Handle error
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
In order to apply a triggered animation to all ToolTip s in my app,
link Im having trouble converting the html entites into html characters, (&# 8217;) i
Does anyone know how can I replace this 2 symbol below from the string
this is what i have right now Drawing an RSS feed into the php,
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
I have some data like this: 1 2 3 4 5 9 2 6
Seemingly simple, but I cannot find anything relevant on the web. What is the
I'm trying to decode HTML entries from here NYTimes.com and I cannot figure out

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.