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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 9, 20262026-06-09T21:45:19+00:00 2026-06-09T21:45:19+00:00

Say you have class A, which classes A1, A2, and A3 inherit from. All

  • 0

Say you have class A, which classes A1, A2, and A3 inherit from. All four classes implement a class method:

+ (NSString *)idString;

Now say you have an object

A *object1; //object may be of type A1, A2, or A3

Is there any way to call the appropriate idString method? I thought maybe:

[[object1 class] idString]

But that won’t compile, says no know class method for selector idString. I also thought of treating idString like a private method and calling

[object1 idString]

But that says no interface declares the selector idString

  • 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-09T21:45:21+00:00Added an answer on June 9, 2026 at 9:45 pm

    Getting the class object of the instance you have, [object class] and sending a message to that, is exactly how this is supposed to work. Classes are objects too in ObjC, and you can send the same message to different classes in exactly the same way you can send intValue to either an NSString or an NSNumber instance (or, init to (nearly) any instance!). This is fundamental to ObjC – the method is looked up based on the message at runtime. If this line:

    [[object1 class] idString];
    

    isn’t compiling, then you have an error elsewhere. That’s completely legal and the way you do what you’re describing.

    #import <Foundation/Foundation.h>
    
    @interface Johnathan : NSObject
    + (NSString *)aNaughtyPhrase;
    @end
    
    @implementation Johnathan
    
    + (NSString *)aNaughtyPhrase
    {
        return @"Knickers";
    }
    
    @end
    
    @interface Johnny : Johnathan @end
    @implementation Johnny
    
    + (NSString *)aNaughtyPhrase
    {
        return @"Botty";
    }
    
    @end
    
    @interface John : Johnathan @end
    @implementation John
    
    + (NSString *)aNaughtyPhrase
    {
        return @"Woo-woo";
    }
    
    @end
    
    @interface Jack : Johnathan @end
    @implementation Jack
    
    + (NSString *)aNaughtyPhrase
    {
        return @"Semprini";
    }
    
    @end
    
    int main(int argc, const char * argv[])
    {
    
        @autoreleasepool {
    
            Johnathan * jt = [Johnathan new];
            Johnny * jy = [Johnny new];
            John * jn = [John new];
            Jack * jk = [Jack new];
    
            NSLog(@"%@", [[jt class] aNaughtyPhrase]);
            NSLog(@"%@", [[jy class] aNaughtyPhrase]);
            NSLog(@"%@", [[jn class] aNaughtyPhrase]);
            NSLog(@"%@", [[jk class] aNaughtyPhrase]);
    
        }
        return 0;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Lets say I have a java package commands which contains classes that all inherit
I have set of classes which inherit from a single super class: Super |
Let's say I have the following class hierarchy: TaskViewer inherits from ListViewer<Task> which in
I have a class which inherits from CListCtrl class, say class list. I have
Say we have a class library project containing 10 classes. Now I want to
Say I have controller X which inherits from controller Y: class X < YController
Say I have a class which represents a person, a variable within that class
Say I have a class Customer which has a property FirstName . Then I
Let's say we have a class foo which has a private instance variable bar
Let's say I have a Size class which has height and width properties (in

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.