GuybrushThreepwood
GuybrushThreepwood

Reputation: 5616

How do I remove all objects from an NSMutableArray?

I need to remove all objects from an NSMutableArray. I can't seem to do this by enumerating as the code crashes.

Can anyone tell me the best way to do this with a code example if possible?

Upvotes: 14

Views: 20602

Answers (5)

Priyank Patel
Priyank Patel

Reputation: 880

1.Create instance of NSMutableArray in .h file

@property (strong, nonatomic) NSMutableArray* arrayEmployee;

2.@synthesize in .m file

@synthesize arrayEmployee;

3.Remove all objects from an NSMutableArray

[self.arrayEmployee removeAllObjects];

Upvotes: 2

Hitesh Chauhan
Hitesh Chauhan

Reputation: 1550

  // In Swift     

 arrayName.removeAllObjects()

Upvotes: -1

In Swift 3:

yourArry.removeAllObjects()

Upvotes: 1

Joel LAO
Joel LAO

Reputation: 11

in case [YourArray removeAllObjects]; doesn't work.

Then do it manually as below:

int c = (int)[YourArray count]-1;
for (int l = 0; l <= c; l++) {[YourArray removeObjectAtIndex:0];}

Upvotes: -2

Richard J. Ross III
Richard J. Ross III

Reputation: 55583

This should do the trick:

[myArray removeAllObjects];

Upvotes: 35

Related Questions