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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:08:29+00:00 2026-06-13T23:08:29+00:00

Why does the method [NSString stringWithFormat] return an id type? From the name I’m

  • 0

Why does the method [NSString stringWithFormat] return an id type? From the name I’m expecting it returns a NSString, not a generic pointer. Other classes follow this “rule”. For example [NSNumber numberWithInt] returns a NSNumber, not an id.

I think it’s not even justified from the fact that is something like a factory method.

  • 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-13T23:08:30+00:00Added an answer on June 13, 2026 at 11:08 pm

    You mention NSNumber, this doesn’t have any direct subclasses, so it’s safe for numberWithInt: to return a NSNumber*

    NSString* (and other classes such as NSSet) return type id because they can have subclasses (NSMutableString and NSMutableSet respectively), which will inherit this method. As such, these inherited calls need to return an instance of the subclass’s type, and due to the rules of method naming, you can’t overload based on return type alone. As such, they need a common type between them all, which is id.

    Update: Recent developments mean the keyword instancetype is now available. You may have noticed a lot of references to id are now replaced with instancetype. This keyword provides a hint to the compiler that the return type of the method is the same class of the receiver of the method.

    For example (and I stress an example, this may not be the case in the actual framework):

    NSArray:
    - (instancetype)initWithArray:(NSArray*)array;
    
    NSMutableArray:
    // Inherits from NSArray
    
    ----
    
    // Receiver is NSArray, method belongs in NSArray, returns an NSArray
    [[NSArray alloc] initWithArray:@[]];
    
    // Receiver is NSMutableArray, method belongs in NSArray, returns an NSMutableArray
    [[NSMutableArray alloc] initWithArray:@[]];
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

A method that overrides another method does not inherit documentation of the method it
The following method does not apply the wpf changes (background = red) until the
The following method does not access any shared variable. It is still not thread
I am not really sure what this method does, or better I am not
Does a method like this exist anywhere in the framework? public static void Swap<T>(ref
Does a method like eval exist in Objective-C as in Javascript, Ruby, PHP, or
Does a method which I check for with respondsToSelector have to actually exist? What
What encryption method does the .NET FormsAuthentication.Encrypt() method use? There's no mention in the
The following method does its job but keeps printing a warning also, im pretty
I'm just to figure out what does this method do, I know there must

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.