We are no longer accepting contributions to Documentation. Please see our post on meta.
This draft deletes the entire topic.
Examples
-
NSMutableArray
can be initialized as an empty array like this:NSMutableArray *array = [[NSMutableArray alloc] init]; // or NSMutableArray *array2 = @[].mutableCopy; // or NSMutableArray *array3 = [NSMutableArray array];
NSMutableArray
can be initialized with another array like this:NSMutableArray *array4 = [[NSMutableArray alloc] initWithArray:anotherArray]; // or NSMutableArray *array5 = anotherArray.mutableCopy;
-
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"red", @"green", @"blue", @"yellow", nil]; NSArray *sortedArray; sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
-
-
NSMutableArray *myColors; myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; [myColors addObject: @"Indigo"]; [myColors addObject: @"Violet"]; //Add objects from an NSArray NSArray *myArray = @[@"Purple",@"Orange"]; [myColors addObjectsFromArray:myArray];
-
Remove at specific index:
[myColors removeObjectAtIndex: 3];
Remove the first instance of a specific object:
[myColors removeObject: @"Red"];
Remove all instances of a specific object:
[myColors removeObjectIdenticalTo: @"Red"];
Remove all objects:
[myColors removeAllObjects];
Remove last object:
[myColors removeLastObject];
-
Using filterUsingPredicate: This Evaluates a given predicate against the arrays content and return objects that match.
Example:
NSMutableArray *array = [NSMutableArray array]; [array setArray:@[@"iOS",@"macOS",@"tvOS"]]; NSPredicate *predicate = [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'i'"]; NSArray *resultArray = [array filteredArrayUsingPredicate:predicate]; NSLog(@"%@",resultArray);
-
NSMutableArray *myColors; int i; int count; myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; [myColors insertObject: @"Indigo" atIndex: 1]; [myColors insertObject: @"Violet" atIndex: 3];
-
Move Blue to the beginning of the array:
NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil]; NSUInteger fromIndex = 2; NSUInteger toIndex = 0; id blue = [[[self.array objectAtIndex:fromIndex] retain] autorelease]; [self.array removeObjectAtIndex:fromIndex]; [self.array insertObject:blue atIndex:toIndex];
myColors
is now[@"Blue", @"Red", @"Green", @"Yellow"]
.
Please consider making a request to improve this example.
Still have a question about NSMutableArray?
Ask Question
Sign up or log in
Save edit as a guest
Join Stack Overflow
Using Google
Using Facebook
Using Email and Password
We recognize you from another Stack Exchange Network site!
Join and Save Draftlang-c