How to initialize an empty mutable array in Objective C How to initialize an empty mutable array in Objective C arrays arrays

How to initialize an empty mutable array in Objective C


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)


Basically, there are three options:

First

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

Second

NSMutableArray *myMutableArray = [NSMutableArray new];

Third

NSMutableArray *myMutableArray = [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;