23

I have a list of objects (trucks) with various attributes that populate a tableview. When you tap them they go to an individual truck page. There is an add button which will add them to the favorite list in another tableview. How do I initialize an empty mutable array in Cocoa?

I have the following code:

-(IBAction)addTruckToFavorites:(id)sender:(FavoritesViewController *)controller
{
    [controller.listOfTrucks addObject: ((Truck_Tracker_AppAppDelegate *)[UIApplication sharedApplication].delegate).selectedTruck];

}
1
  • 1
    You shouldn't make the controller's mutable array public like that. The controller should only make an immutable array available to other objects; for adding a new truck, you should add a method to the controller that does that. Then, instead of getting the listOfTrucks and directly changing it without the controller's knowledge, you tell the controller to make the change. Commented Apr 20, 2012 at 20:42

7 Answers 7

46

Update:

With new syntax you can use:

NSMutableArray *myArray = [NSMutableArray new];

Original answer:

for example:

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

And here you find out why (difference between class and instance method)

Sign up to request clarification or add additional context in comments.

2 Comments

Even if the array is initially empty, if you know that you will be putting a certain number of objects into it, initWithCapacity: may make that population step faster (because the array may initially create its backing storage with space for that many objects).
Moreover, autorelease is not the evil that some people make it out to be. There's nothing wrong with [NSMutableArray array]; it's just not appropriate for this case, assuming that the controller will assign the new array directly to its private _listOfTrucks instance variable.
11

Basically, there are three options:

First

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

Second

NSMutableArray *myMutableArray = [NSMutableArray new];

Third

NSMutableArray *myMutableArray = [NSMutableArray array];

Comments

4

You can also initialize in this way

Another way for Objective C apart from the answer of @NSSam

NSMutableArray *myMutableArray = [@[] mutableCopy];

For Swift

let myArray = NSMutableArray()

OR

let myArray = [].mutableCopy() as! NSMutableArray;

Comments

2
NSMutableArray *arr = [NSMutableArray array];

Comments

2

I use this way to initialize an empty mutable array in Objective C:

NSMutableArray * array = [NSMutableArray arrayWithCapacity:0];

Comments

0

listOfTrucks = [NSMutableArray array]; gives you a new mutable array.

Comments

0

In modern Objective - C it could be even more shorter:

NSArray *array = @[];

1 Comment

This doesn't create a mutable array.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.