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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T12:32:07+00:00 2026-05-22T12:32:07+00:00

Apart from the standard [[MyClass alloc] init] pattern, some objects are built from static

  • 0

Apart from the standard [[MyClass alloc] init] pattern, some objects are built from static methods like MyClass *obj = [MyClass classWithString:@"blabla"]

According to widespread memory management guides (including Apple’s), you’re only responsible for releasing the objects that you alloc.

Can anyone provide me with a template for such methods? How do you return the allocated object ([self alloc]; return self;, perhaps)? How do you make sure that it will be released?

  • 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-22T12:32:08+00:00Added an answer on May 22, 2026 at 12:32 pm

    They are class methods, not static methods1. This specific type, creating autoreleased objects, can be referred to as “factory methods” (formerly also “convenience constructors”), and they are discussed in the Concepts in ObjC Guide. They go something like this:

    + (instancetype)whatsisWithThingummy: (Thingummy *)theThingummy {
        return [[self alloc] initWithThingummy:theThingummy];
    }
    

    Where Whatsis is your class, and Thingummy is another class which your class uses.

    If you’re not compiling with ARC, the convention is to autorelease the instance before returning it.

    The instancetype keyword was introduced by Clang for these kinds of methods; combined with self (which is the class object itself2 in a class method) it allows correct subclass behavior: the method produces an instance of the class which received the message.3 instancetype allows the compiler to do more strict typechecking than id.

    An illustration of this usage in subclasses from the framework: +[NSString stringWithFormat:] returns an NSString instance, whereas +[NSMutableString stringWithFormat:], returns an instance of the subclass NSMutableString, without NSMutableString being required to explicitly override the method.

    As discussed by the [Fundamentals][1] doc, there are other uses for these factory methods, such as accessing a singleton, or appraisal of the necessary memory allocation before it’s performed (possible, but less convenient, with a standard alloc/init pair).


    1“Static methods” in Java or C++, “class methods” in Objective-C. There’s no such thing as static methods in ObjC

    2Whereas in an instance method self is, sensibly, a reference to the instance.

    3Previously, like the usual initialization methods (initWith...), you would have used id as the return type. Using a specific class name unnecessarily forces subclasses to override the method.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Apart from the questionable usefulness of this, I'd like to ask if it is
Apart from performance concerns, should web-based applications be built differently according to the number
Apart from just inserting and parsing text into a blank Word field, is there
Apart from lxml , is anyone aware of Python packages that depend on libxml2
Apart from Sql server profiler ,is there any third party user friendly tool available
Apart from main thread, I've ThinkerThread object _thinker whose finished() signal is connected to
Apart from jQuery, which other JavaScript libraries support Visual Studio2005/2008 intellisense integration?
Apart from using image on the button, how can I write X Y on
Is there any cost/drawback (apart from typing too much) to adorning a class with
The following code works in all browsers apart from IE6... var mylib = {

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.