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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T02:05:52+00:00 2026-05-30T02:05:52+00:00

There are two choices for constructors in Objective C/Cocoa: 1. Class Constructor Product *product

  • 0

There are two choices for constructors in Objective C/Cocoa:

1. Class Constructor

Product *product = [Product productWithIdentifier:@"Chocolate"];
// Use product

2. Alloc/init Constructor

Product *product = [[Product alloc] initWithIdentifier:@"Chocolate"];
// Use product
[product release];

What I do

  • I tend to use the class method just because it looks cleaner and you don’t need a release.
  • I see a lot of alloc/init – what’s the advantage to doing it this way?

My Question

  • Which one is preferable? Or is it just a matter of taste?

Code

For context, the class Product would have the following:

+(Product *)productWithIdentifier:(NSString *)identifier_ {
   return [[[[self class] alloc] initWithIdentifier:identifier] autorelease];
}

-(Product *)initWithIndentifier:(NSString *)identifier_ {
   self = [super init]
   if (self) {
       identifier = identifier_;
   }
   return self;
}
  • 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-30T02:05:53+00:00Added an answer on May 30, 2026 at 2:05 am

    If you’re using ARC, there’s not that much of a difference between the two. If you’re not using ARC, the difference is extremely important.

    The alloc/init combination gives you an owning reference. That means you must release it later on. The classnameWithFoo variant returns a non-owning reference. You may not release it.

    This follows the usual Cocoa naming conventions. All methods return non-owning (autoreleased) instances, except for the methods that start with alloc, copy, mutableCopy and new. These return owning references that you must release.

    Which one to use is mostly a matter of taste. However, if you need temporary objects that you can dispose quickly the alloc variant results in slightly fewer method calls (the autorelease) and in a loop, it also reduces the maximum memory footprint. However, in most cases this reduced cost is neglectable.

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

Sidebar

Related Questions

In many cases, there are two implementation choices: a closure and a callable class.
There are two popular closure styles in javascript. The first I call anonymous constructor
I seemingly have two choices: Make my class implement IDisposable . Create my DbCommand
Up to now I have two choices (maybe there are more outside): Set the
I am trying to compile qt, and am faced with two choices whether use
Using bundler to install a gem from github, there are two obvious choices; https://
i am building a site. There are two choices for me. asp.net mvc 3.0
Currently, there two popular choices which implement the iteratee pattern : The enumerator package
There are two weird operators in C#: the true operator the false operator If
There are two popular naming conventions: vc90/win64/debug/foo.dll foo-vc90-win64-debug.dll Please discuss the problems/benefits associated with

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.