We are no longer accepting contributions to Documentation. Please see our post on meta.

Objective-C Language

NSMutableArray All Versions

1.0
2.0
Modern

This draft deletes the entire topic.

Examples

  • 1

    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; 
    
  • 1
    NSMutableArray *myColors = [NSMutableArray arrayWithObjects: @"red", @"green", @"blue", @"yellow", nil];
    NSArray *sortedArray;
    sortedArray = [myColors sortedArrayUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    
  • 0
    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];
    
  • 0

    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];
    
  • 0

    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);
    
  • 0
    NSMutableArray *myColors;
    int i;
    int count;
    myColors = [NSMutableArray arrayWithObjects: @"Red", @"Green", @"Blue", @"Yellow", nil];
    [myColors insertObject: @"Indigo" atIndex: 1];
    [myColors insertObject: @"Violet" atIndex: 3];
    
  • 0

    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.

Syntax

Syntax

Parameters

Parameters

Remarks

Remarks

Still have a question about NSMutableArray? Ask Question

Topic Outline


    We are no longer accepting contributions to Documentation. Drafts cannot be modified.