NSMutableArray example

Here we have discussed about NSMutableArray and how to add and remove objects in NSMutableArray.

In this section we will discuss about the NSMutableArray with some examples to clarify its use in programming.

NSMutableArray class adds insert and delete operations to handle array, which it inherits from NSArray. NSMutableArray class declares the interface that maintains a changeable collection of objects.

Why do we need to subclass NSMutableArray?

NSMutableArray class maintain a changeable collection of objects. But when storing the elements of collection or receiving information about collection, custom NSArray object is used.

Example of Creating and and adding object in NSMutableArray:

First create a object of NSMutableArray in the header file

NSMutableArray *array;

//And property
@property(nonatomic, retain)NSMutableArray *array;
in the viewDidLoad method(in implementation file) initialize the array and add objects to it ..as given below.

array = [[NSMutableArray alloc]init]; [array addObject:@"uber uns"]; [array addObject:@"Weiterempfehlen"]; [array addObject:@"App Bewerten"];

Example of creating NSMutablearray, adding objects and then removing all the objects:

NSMutableArray *myArray = [[NSMutableArray alloc] init];
[myArray addObject:@"One"];
[myArray addObject:@"Two"];
[myArray removeAllObjects];
[myArray release];