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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T10:54:10+00:00 2026-05-27T10:54:10+00:00

I asked a dumb question the other day ( dumb question ) about the

  • 0

I asked a dumb question the other day (dumb question) about the difference between:

// line1
NSMutableData* myData = [NSMutableData data];
// line2
NSMutableData* myData = [[NSMutableData alloc] init];

It was a dumb question and I didn’t catch my mistake in time. What I meant to ask is, what is the difference between:

// line1 -- added retain
NSMutableData* myData = [[NSMutableData data] retain]; // added retain
// line2
NSMutableData* myData = [[NSMutableData alloc] init];

This could easily still be a dumb question… apologies if that is the case! Is there a real difference? With ARC? I have seen NSXMLParser examples use both methods (some of the Apple examples use [[[NSMutableData alloc] init] autorelease]) and I’m not clear if there is really a difference?

  • 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-27T10:54:11+00:00Added an answer on May 27, 2026 at 10:54 am

    retain cannot be called in ARC compiled code.

    To answer you’re question though, in a non-ARC environment these are virtually the same.

    [NSMutableData data] returns an autoreleased object, by calling retain on it you are taking ownership and are responsible for releasing it at some point.

    [[NSMutableData alloc] init] returns an object whose retain count is equal to 1 and therefore you are the owner and responsible for releasing it when you are finished with it.

    Once again though, retain cannot be used in ARC compiled code, so NSMutableData* myData = [[NSMutableData data] retain]; will not compile.

    And to further clarify, if you are using ARC, you can use either of the following lines and be safe, you do not need to worry about how the object is retained or released.

    // line1 
    NSMutableData* myData = [NSMutableData data];
    // line2
    NSMutableData* myData = [[NSMutableData alloc] init];
    

    Edit

    Also, [[[NSMutableData data] retain] autorelease] this code is rather pointless and excessive. What it says is “Give me an auto-released NSMutableData object using the class method data, retain this auto-released object for me, and add this object I now own back to the auto-release pool.” So essentially [NSMutableData data] achieves the same result in less code and less overhead. If you really have seen a line like this in Apple’s examples I would be surprised.

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

Sidebar

Related Questions

Someone asked me a question via e-mail about integer partitions the other day (as
This is a sequel to my (other) dumb question that I have asked today
In a question I asked about ways to gather pixel data from an image
I asked a similar question about this previously, but I did not specify that
I asked a question earlier about how to go through a directory structure and
Asked in the interview: What is the difference between HTTP servlet and SOAP over
asked the next thing from Heroku support the other day: I have an app
Sorry if this has been asked before or it's a really dumb question, but
I asked yesterday a question about css div positioning, now I solved my issue
I have asked a few question lately about the use of the singleton and

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.