Creating and Initializing a Mutable Array

+ arrayWithCapacity:

Creates and returns an NSMutableArray object with enough allocated memory to initially hold a given number of objects. But this number does not limit the capacity of array.

Example

	NSMutableArray *array = [NSMutableArray arrayWithCapacity:1];
    [array addObject:@"Eezy"];
    [array addObject:@"Tutorials"];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(capacity:1)
    array.addObject("Eezy")
    array.addObject("Tutorials")
    print(array)


Output

2014-03-22 15:39:26.868 iOS-Tutorial[1928:a0b] (
    Eezy,
    Tutorials
)

- initWithCapacity:

Returns an array, initialized with enough memory to initially hold a given number of objects.

Example

	var array:NSMutableArray = NSMutableArray(capacity:1)
    array.addObject("Eezy")
    array.addObject("Tutorials")
    print(array)
// implementation in progress


Output

2014-03-22 15:42:03.199 iOS-Tutorial[1953:a0b] (
    Eezy,
    Tutorials
)

- init

Initializes a newly allocated array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]init];
    NSLog(@"%@",array);
	var array:NSMutableArray = NSMutableArray()
    print(array)


Output

2014-03-22 15:43:08.648 iOS-Tutorial[1974:a0b] (
)

Adding Objects

- addObject:

Inserts a given object at the end of the array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithCapacity:1];
    [array addObject:@"Eezy"];
    [array addObject:@"Tutorials"];
    NSLog(@"%@",array);
	var array:NSMutableArray = NSMutableArray(capacity:1)
    array.addObject("Eezy")
    array.addObject("Tutorials")
    print(array)


Output

2014-03-22 15:42:03.199 iOS-Tutorial[1953:a0b] (
    Eezy,
    Tutorials
)

- addObjectsFromArray:

Adds the objects contained in another given array to the end of the receiving array’s content.

Example

	NSMutableArray *array = [[NSMutableArray alloc]init];
    [array addObjectsFromArray:@[@"Eezy",@"Tutorials"]];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(capacity:1)
    array.addObjectsFromArray(["Eezy", "Tutorials"])
    print(array)


Output

2014-03-22 15:46:09.374 iOS-Tutorial[1992:a0b] (
    Eezy,
    Tutorials
)

- insertObject:atIndex:

Inserts a given object into the array's contents at a given index.

Example

	NSMutableArray *array = [[NSMutableArray alloc]init];
    [array addObjectsFromArray:@[@"Eezy",@"Tutorials"]];
    [array insertObject:@"Website" atIndex:1];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(capacity:1)
	array.addObjectsFromArray(["Eezy", "Tutorials"])
	array.insertObject("Website", atIndex: 1)
	print(array)


Output

Tutorial[2008:a0b] (
    Eezy,
    Website,
    Tutorials
)

- insertObjects:atIndexes:

Inserts the objects in the provided array into the receiving array at the specified indexes.

Example

	NSMutableArray *array = [[NSMutableArray alloc]init];
	[array insertObjects:@[@"Eezy",@"Tutorials"] atIndexes:
  	[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(0,2)]];
	NSLog(@"%@",array);
	var array:NSMutableArray = NSMutableArray(capacity:1)
	array.insertObjects(["Eezy", "Tutorials"], atIndexes: NSIndexSet(indexesInRange: NSMakeRange(0, 2)))
	print(array)


Output

2014-03-22 15:52:55.139 iOS-Tutorial[2044:a0b] (
    Eezy,
    Tutorials
)

Removing Objects

- removeAllObjects

p>Empties the array of all its elements.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials"]];
    [array removeAllObjects];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(array: ["Eezy", "Tutorials"])
	array.removeAllObjects()
	print(array)


Output

2014-03-22 15:56:05.336 iOS-Tutorial[2078:a0b] (
)

- removeLastObject

Removes the object with the highest-valued index in the removeObjectsFromIndices array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials"]];
    [array removeLastObject];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(array: ["Eezy", "Tutorials"])
	array.removeLastObject()
	print(array)


Output

2014-03-22 15:58:04.391 iOS-Tutorial[2094:a0b] (
    Eezy
)

- removeObject:

Removes all occurrences in the array of a given object.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Eezy"]];
    [array removeObject:@"Eezy"];
    NSLog(@"%@",array);
	var array:NSMutableArray = NSMutableArray(array: ["Eezy", "Tutorials", "Eezy"])
	array.removeObject("Eezy")
	print(array)


Output

2014-03-22 15:58:04.391 iOS-Tutorial[2110:a0b] (
    Tutorials
)

- removeObject:inRange:

Removes all occurrences within a specified range in the array of a given object.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Eezy"]];
    [array removeObjectsInRange:NSMakeRange(1, 1)];
    NSLog(@"%@",array);
	var array:NSMutableArray = NSMutableArray(array: ["Eezy", "Tutorials", "Eezy"])
	array.removeObjectsInRange(NSMakeRange(1, 1))
	print(array)


Output

2014-03-22 16:00:05.876 iOS-Tutorial[2127:a0b] (
    Eezy,
    Eezy
)

- removeObjectAtIndex:

Removes the object at index .

Example

	NSMutableArray *array = [[NSMutableArray alloc]
		initWithArray:@[@"Eezy",@"Tutorials",@"Eezy"]];
    [array removeObjectAtIndex:2];
    NSLog(@"%@",array);
	var array:NSMutableArray = NSMutableArray(array: ["Eezy", "Tutorials", "Eezy"])
	array.removeObjectAtIndex(2)
	print(array)


Output

2014-03-22 16:01:15.507 iOS-Tutorial[2140:a0b] (
    Eezy,
    Tutorials
)

- removeObjectsAtIndexes:

Removes the objects at the specified indexes from the array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Eezy"]];
    [array removeObjectsAtIndexes:[NSIndexSet indexSetWithIndex:1]];
    NSLog(@"%@",array);
	var array:NSMutableArray = NSMutableArray(array: ["Eezy", "Tutorials", "Eezy"])
	array.removeObjectsAtIndexes(NSIndexSet(indexesInRange: NSMakeRange(1, 1)))
	print(array)


Output

2014-03-22 16:03:43.887 iOS-Tutorial[2160:a0b] (
    Eezy,
    Eezy
)

- removeObjectIdenticalTo:

Removes all occurrences of a given object in the array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Eezy"]];
    [array removeObjectIdenticalTo:@"Eezy"];
    NSLog(@"%@",array);		
	var str:NSString = "Eezy"
	var array:NSMutableArray = NSMutableArray(array: [str, "Tutorials", str])
	array.removeObjectIdenticalTo(str)
	print(array)


Output

2014-03-22 16:45:05.113 iOS-Tutorial[2257:a0b] (
    Tutorials
)

- removeObjectIdenticalTo:inRange:

Removes all occurrences of anObject within the specified range in the array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Eezy"]];
    [array removeObjectIdenticalTo:@"Eezy" inRange:NSMakeRange(0, 2)];
    NSLog(@"%@",array)
	var str:NSString = "Eezy"
	var array:NSMutableArray = NSMutableArray(array: [str, "Tutorials", str])
	array.removeObjectIdenticalTo(str,inRange:NSMakeRange(0, 2))
	print(array)


Output

2014-03-22 16:46:49.555 iOS-Tutorial[2273:a0b] (
    Tutorials,
    Eezy
)

- removeObjectsInArray:

Removes from the receiving array the objects in another given array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array removeObjectsInArray:@[@"Eezy"]];
    NSLog(@"%@",array);		
	var str:NSString = "Eezy"
	var array:NSMutableArray = NSMutableArray(array: [str, "Tutorials", "Website"])
	array.removeObjectsInArray([str])
	print(array)


Output

2014-03-22 16:48:18.219 iOS-Tutorial[2286:a0b] (
    Tutorials,
    Website
)

- removeObjectsInRange:

Removes from the array each of the objects within a given range.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array removeObjectsInRange:NSMakeRange(0, 2)];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(array: ["Eezy","Tutorials","Website"])
	array.removeObjectsInRange(NSMakeRange(0, 2))
	print(array);


Output

2014-03-22 16:50:30.993 iOS-Tutorial[2300:a0b] (
    Website
)

Replacing Objects

- replaceObjectAtIndex:withObject:

Replaces the object at index with anObject.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array replaceObjectAtIndex:0 withObject:@"iOS"];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(array: ["Eezy","Tutorials","Website"])
	array.replaceObjectAtIndex(0, withObject: "iOS")
	print(array);


Output

2014-03-22 16:53:07.669 iOS-Tutorial[2317:a0b] (
    iOS,
    Tutorials,
    Website
)

- setObject:atIndexedSubscript:

Replaces the object at the index with the new object, possibly adding the object.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array setObject:@"iOS" atIndexedSubscript:0];
    NSLog(@"%@",array);		
// Not available in swift


Output

2014-03-22 16:54:42.019 iOS-Tutorial[2331:a0b] (
    iOS,
    Tutorials,
    Website
)

- replaceObjectsAtIndexes:withObjects:

Replaces the objects in the receiving array at specified locations specified with the objects from a given array.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array replaceObjectsAtIndexes:[NSIndexSet indexSetWithIndex:0] withObjects:@[@"iOS"]];
    NSLog(@"%@",array);		
	var array:NSMutableArray = NSMutableArray(array: ["Eezy","Tutorials","Website"])
	array.replaceObjectsAtIndexes(NSIndexSet(index: 0), withObjects: ["iOS"])
	print(array);


Output

2014-03-22 16:57:00.083 iOS-Tutorial[2344:a0b] (
    iOS,
    Tutorials,
    Website
)

- replaceObjectsInRange:withObjectsFromArray:range:

Replaces the objects in the receiving array specified by one given range with the objects in another array specified by another range.

Example

	NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array replaceObjectsInRange:NSMakeRange(0, 1) 
		withObjectsFromArray:@[@"iOS",@"Basic"] 
		range:NSMakeRange(1, 1)];
    NSLog(@"%@",array);
// implementation in progress


Output

2014-03-22 17:01:37.288 iOS-Tutorial[2375:a0b] (
    Basic,
    Tutorials,
    Website
)

- replaceObjectsInRange:withObjectsFromArray:

Replaces the objects in the receiving array specified by a given range with all of the objects from a given array.

Example

	 NSMutableArray *array = [[NSMutableArray alloc]initWithArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array replaceObjectsInRange:NSMakeRange(0, 1) withObjectsFromArray:@[@"iOS"]];
    NSLog(@"%@",array);
// implementation in progress


Output

2014-03-22 16:59:39.297 iOS-Tutorial[2360:a0b] (
    iOS,
    Tutorials,
    Website
)

- setArray:

Sets the receiving array’s elements to those in another given array.

Example

	NSMutableArray *array = [NSMutableArray array];
    [array setArray:@[@"Eezy",@"Tutorials",@"Website"]];
    NSLog(@"%@",array);		
// implementation in progress


Output

2014-03-22 17:04:28.739 iOS-Tutorial[2387:a0b] (
    Eezy,
    Tutorials,
    Website
)

Filtering Content

- filterUsingPredicate:

Evaluates a given predicate against the array’s content and leaves only objects that match.

Example

	NSMutableArray *array = [NSMutableArray array];
    [array setArray:@[@"Eezy",@"Tutorials",@"Website"]];
    NSPredicate *predicate =
    [NSPredicate predicateWithFormat:@"SELF beginswith[c] 'E'"];
    NSArray *resultArray = [array filteredArrayUsingPredicate:predicate];
    NSLog(@"%@",resultArray);
// implementation in progress


Output

2014-03-22 17:12:15.625 iOS-Tutorial[2419:a0b] (
    Eezy
)

Rearranging Content

- exchangeObjectAtIndex:withObjectAtIndex:

Exchanges the objects in the array at given indices.

Example

	NSMutableArray *array = [NSMutableArray array];
    [array setArray:@[@"Eezy",@"Tutorials",@"Website"]];
    [array exchangeObjectAtIndex:0 withObjectAtIndex:2];
    NSLog(@"%@",array);		
// implementation in progress


Output

2014-03-22 17:14:06.190 iOS-Tutorial[2445:a0b] (
    Website,
    Tutorials,
    Eezy
)

- sortUsingDescriptors:

Sorts the receiving array using a given array of sort descriptors.

Example

	NSDictionary *dict1 = @{@"key1":@"iOS",@"key2": @"Tutorials", @"key3":@"Website"};
    NSDictionary *dict2 = @{@"key1":@"Eezy",@"key2": @"Tutorials", @"key3":@"Website"};

    NSMutableArray *array = [NSMutableArray arrayWithObjects:dict1,dict2, nil];
    NSSortDescriptor * descriptor =[[NSSortDescriptor alloc] initWithKey:@"key1"
    ascending:YES];
    NSArray *resultArray = [array sortedArrayUsingDescriptors:@[descriptor]];
    NSLog(@"%@",resultArray);
// implementation in progress


Output

2014-03-22 17:30:38.893 iOS-Tutorial[2518:a0b] (
        {
        key1 = Eezy;
        key2 = Tutorials;
        key3 = Website;
    },
        {
        key1 = iOS;
        key2 = Tutorials;
        key3 = Website;
    }
)

- sortUsingComparator:

Sorts the array using the comparison method specified by a given NSComparator Block.

Example

 	NSDictionary *dict1 = @{@"key1":@"iOS",@"key2": @"Tutorials", @"key3":@"Website"};
    NSDictionary *dict2 = @{@"key1":@"Eezy",@"key2": @"Tutorials", @"key3":@"Website"};

    NSMutableArray *array = [NSMutableArray arrayWithObjects:dict1,dict2, nil];
   
    NSArray *resultArray = [array sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [[obj1 objectForKey:@"key1"] localizedStandardCompare:[obj2 objectForKey:@"key1"]];
    }];
    NSLog(@"%@",resultArray);
// implementation in progress


Output

2014-03-22 17:40:12.008 iOS-Tutorial[2603:a0b] (
        {
        key1 = Eezy;
        key2 = Tutorials;
        key3 = Website;
    },
        {
        key1 = iOS;
        key2 = Tutorials;
        key3 = Website;
    }
)

- sortWithOptions:usingComparator:

Sorts the array using the specified options and the comparison method specified by a given NSComparator Block.

Example

	NSDictionary *dict1 = @{@"key1":@"iOS",@"key2": @"Tutorials", @"key3":@"Website"};
    NSDictionary *dict2 = @{@"key1":@"Eezy",@"key2": @"Tutorials", @"key3":@"Website"};

    NSMutableArray *array = [NSMutableArray arrayWithObjects:dict1,dict2, nil];
   
   [array sortWithOptions:NSSortStable usingComparator:^NSComparisonResult(id obj1, id obj2) {
        return [[obj1 objectForKey:@"key1"] localizedStandardCompare:[obj2 objectForKey:@"key1"]];
    }];
    NSLog(@"%@",array);		
// implementation in progress


Output

2014-03-22 17:42:56.093 iOS-Tutorial[2617:a0b] (
        {
        key1 = Eezy;
        key2 = Tutorials;
        key3 = Website;
    },
        {
        key1 = iOS;
        key2 = Tutorials;
        key3 = Website;
    }
)

- sortUsingFunction:context:

Sorts the array’s elements in ascending order as defined by the comparison function compare.

Example

- (void)sortExample{
    NSDictionary *dict1 = @{@"key1":@"iOS",@"key2": @"Tutorials", @"key3":@"Website"};
    NSDictionary *dict2 = @{@"key1":@"Eezy",@"key2": @"Tutorials", @"key3":@"Website"};
    
    NSMutableArray *array = [NSMutableArray arrayWithObjects:dict1,dict2, nil];
    
    [array sortUsingFunction:sort context:nil];
    NSLog(@"%@",array);
}

NSInteger sort(id obj1, id obj2, void *context)
{
    
    return ([[obj1 objectForKey:@"key1" ] compare:[obj2 objectForKey:@"key1" ]]);
}
// implementation in progress


Output

2014-03-22 17:51:31.207 iOS-Tutorial[2636:a0b] (
        {
        key1 = Eezy;
        key2 = Tutorials;
        key3 = Website;
    },
        {
        key1 = iOS;
        key2 = Tutorials;
        key3 = Website;
    }
)

- sortUsingSelector:

Sorts the array’s elements in ascending order, as determined by the comparison method specified by a given selector.

Example

	NSMutableArray *array = [NSMutableArray arrayWithObjects:@"Tutorials",@"tutorials",@"Eezy",@"eezy", nil];
    [array sortUsingSelector:@selector(compare:)];
    NSLog(@"%@",array);
    [array sortUsingSelector:@selector(localizedCaseInsensitiveCompare:)];
    NSLog(@"%@",array);
    [array sortUsingSelector:@selector(localizedCompare:)];
    NSLog(@"%@",array);		
// implementation in progress


Output

2014-03-22 17:58:01.936 iOS-Tutorial[2685:a0b] (
    Eezy,
    Tutorials,
    eezy,
    tutorials
)
2014-03-22 17:58:01.938 iOS-Tutorial[2685:a0b] (
    Eezy,
    eezy,
    Tutorials,
    tutorials
)
2014-03-22 17:58:01.938 iOS-Tutorial[2685:a0b] (
    eezy,
    Eezy,
    tutorials,
    Tutorials
)

Advertisements