How to initialize an empty mutable array in Objective-C

I have a list of objects (trucks) with various attributes that populate the table. When you click on them, they go to a separate page of the truck. There is an add button that will add them to your favorites in another table. How to 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];

}
+5
source share
6 answers

Update:

With the new syntax, you can use:

NSMutableArray *myArray = [NSMutableArray new];

Original answer:

eg:

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

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

+19
source

Basically, there are three options:

First

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

Second

NSMutableArray *myMutableArray = [NSMutableArray new];

Third

NSMutableArray *myMutableArray = [NSMutableArray array];
+2
source
NSMutableArray *arr = [NSMutableArray array];
+1

Objective C, @NSSam

NSMutableArray *myMutableArray = [@[] mutableCopy];

For swift

let myArray = NSMutableArray()

OR

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

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

0
source
NSMutableArray *arr = [NSMutableArray new];
0
source

All Articles