How to initialize an empty mutable array in Objective C
How to initialize an empty mutable array in Objective C
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];
}
listOfTrucks
8 Answers
8
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)
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
initWithCapacity:
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
[NSMutableArray array]
_listOfTrucks
Basically, there are three options:
First
NSMutableArray *myMutableArray = [[NSMutableArray alloc] init];
Second
NSMutableArray *myMutableArray = [NSMutableArray new];
Third
NSMutableArray *myMutableArray = [NSMutableArray array];
NSMutableArray *arr = [NSMutableArray array];
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;
listOfTrucks = [NSMutableArray array]; gives you a new mutable array.
listOfTrucks = [NSMutableArray array];
NSMutableArray *arr = [NSMutableArray new];
I use this way to initialize an empty mutable array in Objective C:
NSMutableArray * array = [NSMutableArray arrayWithCapacity:0];
In modern Objective - C it could be even more shorter:
NSArray *array = @
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
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
listOfTrucksand directly changing it without the controller's knowledge, you tell the controller to make the change.– Peter Hosey
Apr 20 '12 at 20:42