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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:57:47+00:00 2026-05-16T10:57:47+00:00

Is it possible to set the return type of a selector in objective-c to

  • 0

Is it possible to set the return type of a selector in objective-c to be the class itself?

For example, is it possible to do something like:

+ ([self class] *) selectorName;

So that given a super class such as:

@interface MyClass : NSObject {}
+ (MyClass *) instanceOfMyClass;
@end

@implementation MyClass 
+ (MyClass *) instanceOfMyClass {
  return [[[[self class] alloc] init] autorelease];
}
@end

The subclass does not need to cast the return value when doing things like:

@implementation MySubClass 
+ (MySubClass *) specialInstanceOfMySubClass {
  MySubClass *instance = [[self class] instanceOfMyClass];
  instance.special = YES;
  return instance;
}
@end

In other words, im trying to expose the fact that the static method returns an instance of the class or its subclass. Otherwise, the subclass needs to be aware of information not provided in the interface.

Thanks!

  • 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-16T10:57:47+00:00Added an answer on May 16, 2026 at 10:57 am

    There is no such thing. There are basically two ways to type variables in Objective-C:

    • static: like you already did: ClassName *
    • anonymous: use the generic type id that can match any object

    So in your case you could do a method declaration like this:

    + (id)instanceOfMyClass;
    

    The convention is to return an instance of that class. Apple uses this convention in basically every class. If the result is different, you should write it in the documentation for this method. That’s also the way Apple does this.

    Another option would be to cast the result in you subclass’ method.

    MySubClass *instance = (MySubClass *)[[self class] instanceOfMyClass];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is it possible to set custom return key type for UITextField ? [txtField setReturnKeyType:UIReturnKeyDefault];
Is it possible to set code behind a resource dictionary in WPF. For example
I have a collection of objects of one type that I'd like to convert
I would like to know if in SQL is it possible to return a
Is possible to set an image as an iPhone application unique icon in code
Is it possible to set the cursor to 'wait' on the entire html page
Is it possible to set the data in a ContentPlaceHolder through code? This is
Is it possible to set the position of the tabs to be at the
Is it possible to set up a Grid or other container control to be
Is it possible to set some image as title of Navigation bar? I think

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.