Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
  • NSArray with different functions and operations.

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 683
    Comment on it

    NSArray and NSMutableArray are Objective-Cs general array types. It represents an ordered collection of objects, and it provides a high-level interface for sorting and manipulating lists of data.

    NSArray are immutable type where NSMutableArray is mutable type, in short we can say NSMutableArray can be modified(add or remove) but NSArray can't be.

    Array Initialization

    NSArray *carManufacturers = [NSArray arrayWithObjects:@"BMW", @"Ford", @"Jaguar", @"AstonMartin", nil];

    NSMutableArray *cars = [NSMutableArray arrayWithObjects: @"Ford", @"Jaguar", @"AstonMartin",@"BMW", nil];

    Sorting NSArray

        NSArray *sortedArrays = [carManufacturers sortedArrayUsingComparator:
        ^NSComparisonResult(id obj1, id obj2) {
            if ([obj1 length] < [obj2 length]) {
                return NSOrderedAscending;
            } else if ([obj1 length] > [obj2 length]) {
                return NSOrderedDescending;
            } else {
                return NSOrderedSame;
    NSLog(@"%@", sortedMakes);
    **Adding Arrays**
    NSArray *carManufacturers = [NSArray arrayWithObjects:@"BMW",
                        @"Ford", @"Jaguar", @"AstonMartin", nil];
    NSArray *bikeManufacturers = @[@"Honda", @"Yamaha", @"SUZUKI", @"Harley"];
    NSArray *allManufacturers = [carManufacturers bikeManufacturers];
    NSLog(@"%@", allManufacturers);

    Array to String Conversion

    NSArray *carManufacturers = [NSArray arrayWithObjects:@"BMW",
                        @"Ford", @"Jaguar", @"AstonMartin", nil];
    NSLog(@"%@", [carManufacturers componentsJoinedByString:@", "]);

    SubDivide a NSArray

    NSArray *topCars = [NSArray arrayWithObjects:@"BMW",
                        @"Audi", @"Jaguar", @"AstonMartin", nil];
    NSArray *allCars = @[@"Mercedes-Benz", @"Porsche",
                             @"Opel", @"Volkswagen", @"Ford"];
    NSArray *lastTwo = [topCars subarrayWithRange:NSMakeRange(4, 2)];
    NSLog(@"%@", lastTwo);

    NSMutableArray Add/Remove

    NSMutableArray *cars = [NSMutableArray arrayWithObjects:
                                  @"Ford", @"Jaguar", @"AstonMartin",@"BMW", nil];
    [cars addObject:@"BMW F25"];
    NSLog(@"%@", cars);       // BMW F25 added to end
    [cars removeLastObject];
    NSLog(@"%@", cars);       // BMW F25 removed from end

    Add at some define or arbitrary location

    // Add BMW F25 to front
    [cars insertObject:@"BMW F25" atIndex:0];
    // Remove BMW F25 from front
    [cars removeObjectAtIndex:0];
    // Remove Audi Quattro
    [cars removeObject:@"Audi Quattro"];

    Remove All Objects

    [cars removeAllObjects];

    Replacing an Object

    [cars replaceObjectAtIndex:1 withObject:@"Honda"];

    *Find an Object using contain *

    if ([cars containsObject:@"BMW"]){
         NSLog(@"Found BMW")

    *Happy Coding :)

 0 Comment(s)

Sign In

Sign up using

Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: