NSMutableArray arrayWithCapacity vs initWithCapacity

I am new to iPhone / Objective-C with an extensive Java background.

I learn more about memory management in objective-c, and I read Apple's memory management documentation: http://developer.apple.com/iphone/library/documentation/Cocoa/Conceptual/MemoryMgmt/MemoryMgmt.html p>

The Object Ownership Policy section says that you own any object created using a method that begins by highlighting, new, or contains a copy. Ownership implies that you need an explicit releaseobject when you are done with it.

So, I look at the NSMutableArray documentation: http://developer.apple.com/mac/library/documentation/Cocoa/Reference/Foundation/Classes/NSMutableArray_Class/Reference/Reference.html

There are two methods that pretty much do the same thing: they create an array with some initial capacity. One of them is a class method, and the other is an instance method.

+ (id)arrayWithCapacity:(NSUInteger)numItems;
- (id)initWithCapacity:(NSUInteger)numItems;

Now, being a lazy Java developer, why did I ever choose an instance method over a class method, knowing that at some point in time I should clear myself after?

, ... , ? autorelease vs. release ? , (iPhone) , ?

!

+5
3

, , (, - ivar). alloc/init, , . - , , , .

, , , alloc/init , /, alloc/init/autorelease , .

alloc/init / .

+9

arrayWithCapacity: .

initWithCapacity: , . [[A alloc] init...], " ", , "alloc", "" "" . , , .

, , , init/release. , autorelease factory , .

, - , ​​ NSDictionary NSArray, "-" autorelease factory, " " . ( .)

,

Blah *blah = [Blah blahWithSomething];
[myMutableArray addObject:blah];

,

Blah *blah = [[Blah alloc] initWithSomething];
[myMutableArray addObject:blah];
[blah release];

. , , runloop, , / , runloop. , , , .

+6

, , (== " " ):

  • , factory , , / .
  • ,
0

All Articles