1

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?

Community
  • 1
  • 1
Raolin
  • 379
  • 1
  • 4
  • 14
  • 1
    Compiler does not let you use `release` with ARC. – Sergey Kalinichenko Dec 12 '11 at 06:28
  • duplicate of [Difference between \[\[NSDate date\] retain\] and \[\[NSDate alloc\] init\]](http://stackoverflow.com/questions/7102706/difference-between-nsdate-date-retain-and-nsdate-alloc-init) – jscs Dec 12 '11 at 08:20
  • 1
    @JoshCaswell: ARC didn't exist back then, and this questioner is specifically asking about the difference under ARC. That changes the answer: Under ARC, the difference is that one is invalid. – Peter Hosey Dec 12 '11 at 15:01

1 Answers1

7

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.

Peter Hosey
  • 95,783
  • 15
  • 211
  • 370
Chris Wagner
  • 20,773
  • 8
  • 74
  • 95
  • Sorry, you're absolutely right, I pasted the wrong code. The Apple examples do a proper alloc-init-autorelease...not the data-retain. I'll update my question. – Raolin Dec 12 '11 at 07:02
  • Chris is right,in addition ,you can use also [NSMutableData new] it's the same as [[NSMutableData alloc] init]. – NadavN7 Dec 12 '11 at 07:19