0

Possible Duplicate:
Diference between [NSMutableArray array] vs [[NSMutableArray alloc] init]

Using Objective-C/Cocoa, what's the difference between:

NSMutableData *myData = [NSMutableData data];
NSMutableString *myString = [NSMutableString string];

and

NSMutableData *myData = [[NSMutableData alloc] init];
NSMutableString *myString = [[NSMutableString alloc] init];

They seem to have the same end result as far as I can tell?

Community
  • 1
  • 1
Raolin
  • 379
  • 1
  • 4
  • 14
  • 2
    Have a look at ["Object Creation"](http://developer.apple.com/library/ios/documentation/General/Conceptual/DevPedia-CocoaCore/ObjectCreation.html#//apple_ref/doc/uid/TP40008195-CH39-SW1) in the Cocoa Core Competencies Guide. – jscs Dec 10 '11 at 03:14
  • 1
    look @ the apple memory guide - important read. alloc, copy, mutableCopy means you own it and you need to release. Anything else (by convention) calls autorelease and will get released on next cycle ... – bryanmac Dec 10 '11 at 03:16
  • 1
    if you need to hold onto something that's autoreleased outside of the scope of the function that you got it, then you should retain it and then release later. – bryanmac Dec 10 '11 at 03:17

1 Answers1

-1

[NSMutableData data] is referred to as a helper, check this post, Helper functions in Cocoa

Helpers generally take care of the memory management for your, can also be used to return singletons.

[[NSMutableData] alloc] init], you are responsible for memory management.

Although if you are using ARC you don't have to release your objects, it does it for you.

Community
  • 1
  • 1
Devraj
  • 3,025
  • 24
  • 25
  • 5
    The post you linked to doesn't have anything to do with this. These methods are "convenience constructors". – jscs Dec 10 '11 at 03:13