How are these NSMutableArray initializations different?

In my code branch, I previously used this

NSMutableArray *array1 = [[NSMutableArray alloc] init];

      

The given array is used to populate the UITableVew.

I just came to the following:

NSMutableArray *array1 = [NSMutableArray arrayWithCapacity:0]

      

I haven't made any changes to my code) and my application crashes when I try to scroll down the list in the UITableView.

It looks like my array is not initialized correctly. Can someone explain why this is happening? Are these two methods not identical how the underlying memory space is allocated?

+2


source to share


1 answer


The second line of code does not hold the NSArray, which causes it to crash. You will need to call [array1 retain]

after the call arrayWithCapacity:

.

There is quite a bit of useful information in this post: Understanding Reference Counting with Cocoa / Objective C



In general, if you call a class method that does not start with "new" or "init" (for example arrayWithCapacity

), you can usually assume that the returned object will be auto-implemented.

+2


source







All Articles