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];

}
crashprophet
  • 327
  • 1
  • 4
  • 13
  • 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. – Peter Hosey Apr 20 '12 at 20:42

7 Answers7

44

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)

Community
  • 1
  • 1
Jakub
  • 13,712
  • 17
  • 82
  • 139
  • 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). – Peter Hosey Apr 20 '12 at 20:37
  • 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. – Peter Hosey Apr 20 '12 at 20:38
11

Basically, there are three options:

First

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

Second

NSMutableArray *myMutableArray = [NSMutableArray new];

Third

NSMutableArray *myMutableArray = [NSMutableArray array];
Jianxin Gao
  • 2,717
  • 2
  • 19
  • 32
Sangram Shivankar
  • 3,535
  • 3
  • 26
  • 38
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;
Janmenjaya
  • 4,149
  • 1
  • 23
  • 43
2
NSMutableArray *arr = [NSMutableArray array];
fbernardo
  • 10,016
  • 3
  • 33
  • 46
2

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

NSMutableArray * array = [NSMutableArray arrayWithCapacity:0];
Agisight
  • 1,778
  • 1
  • 14
  • 15
0

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

jtbandes
  • 115,675
  • 35
  • 233
  • 266
-1

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

NSArray *array = @[];
GeRyCh
  • 1,580
  • 3
  • 13
  • 23