Advertisements
NSArray by example
In this tutorial, we will be covering various functions available in NSArray. NSArray help us in creating static array.
Modern Objective C Array notations.
There is modern Objective C syntax that makes it easier for us to create and retrieve objects. Xcode actually changes to required format in build time. The syntax for initialization and accessing is as shown below.
// initialization NSArray *array = @[@"Eezy", @"Tutorials"]; // access value at index 0 NSString *value = array[0];
Instance Methods
+ array
This method allows us creating a static empty array.
Example
NSArray *array = [NSArray array]; NSLog(@"%@",array);
Output
2014-01-27 19:53:51.555 array[25037] ()
+ arrayWithObject:
This method allows us creating a static array with one object.
Example
NSArray *array = [NSArray arrayWithObject:@"Eezy"]; NSLog(@"%@",array);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy )
+ arrayWithObjects:
This method allows us creating a static array with one object.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials"]; NSLog(@"%@",array);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
+ arrayWithArray:
This method allows us creating a static empty array.
Example
NSArray *tempArray = [NSArray arrayWithObjects:@"Eezy",@"Tutorials"]; NSArray *array = [NSArray arrayWithArray:tempArray]; NSLog(@"%@",array);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
+ arrayWithContentsOfFile:
This method allows us to read an array from a apple propery list(plist) file.
Assuming we have the following data in plist in application bundle that is the plist is added to our project.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <string>Eezy</string> <string>Tutorials</string> </array> </plist>
Example
NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; NSArray *array = [NSArray arrayWithContentsOfFile:file]; NSLog(@"%@", array);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
+ arrayWithContentsOfURL:
Creates and returns an array containing the contents specified by a given URL.
Example
NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist"]; NSArray *array = [NSArray arrayWithContentsOfURL:url]; NSLog(@"%@",array ]);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
+ arrayWithObjects:count:
Creates and returns an array that includes a given number of objects from a given C array.
Example
NSString *values[3]; values[0] = @"Eezy"; values[1] = @"Tutorials"; values[2] = @"Website"; // Website ignored since count is 2 NSArray *array = [NSArray arrayWithObjects:values count:2]; NSLog(@"%@",array ]);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
Initializing an Array
- init
Initializes a newly allocated array. Recommended use is [NSArray array] instead of [[NSArray alloc]init]
Example
NSArray *array = [[NSArray alloc]init]; NSLog(@"%@",array ]);
Output
2014-01-27 19:53:51.555 array[25037] ( )
- initWithArray:
Initializes a newly allocated array by placing in it the objects contained in a given array. Recommended use is [NSArray arrayWithArray:] instead of [[NSArray alloc]initWithArray:]
Example
var tempArray:NSArray = NSArray(array: ["Eezy","Tutorials"]) var array = NSArray(array: tempArray) println(array)
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
- initWithArray:copyItems:
Initializes a newly allocated array using anArray as the source of data objects for the array.
Example
NSArray *tempArray = [NSArray arrayWithObjects:@"Eezy",@"Tutorials"]; // Check if the two array objects refer to same objects. It shouldn't. NSArray *array = [[NSArray alloc]initWithArray:tempArray copyItems:YES]; NSLog(@"%@",array);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
- initWithContentsOfFile:
Initializes a newly allocated array with the contents of the file specified by a given path. Recommended use is [NSArray arrayWithContentsOfFile:] instead of [[NSArray alloc]initWithContentsOfFile:]
Assuming we have the following data in plist in application bundle that is the plist is added to our project.
<?xml version="1.0" encoding="UTF-8"?> <!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd"> <plist version="1.0"> <array> <string>Eezy</string> <string>Tutorials</string> </array> </plist>
Example
NSString *file = [[NSBundle mainBundle] pathForResource:@"Data" ofType:@"plist"]; NSArray *array = [[NSArray alloc]initWithContentsOfFile:file]; NSLog(@"%@", array);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
- initWithContentsOfURL:
Initializes a newly allocated array with the contents of the location specified by a given URL. Recommended use is [NSArray arrayWithContentsOfURL:] instead of [[NSArray alloc]initWithContentsOfURL:]
Example
NSURL *url = [NSURL URLWithString:@"http://ios.eezytutorials.com/sample-files/sample-array-plist.plist"]; NSArray *array = [[NSArray alloc]initWithContentsOfURL:url]; NSLog(@"%@",array ]);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
- initWithObjects:
Initializes a newly allocated array by placing in it the objects in the argument list. Recommended use is [NSArray arrayWithObjects:] instead of [[NSArray alloc]initWithObjects:]
Example
NSArray *array = [[NSArray alloc] initWithObjects:@"Eezy",@"Tutorials"]; NSLog(@"%@",array)
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
- initWithObjects:count:
Initializes a newly allocated array to include a given number of objects from a given C array. Recommended use is [NSArray arrayWithObjects:count:] instead of [[NSArray alloc]initWithObjects:count:]
Example
NSString *values[3]; values[0] = @"Eezy"; values[1] = @"Tutorials"; values[2] = @"Website"; // Website ignored since count is 2 NSArray *array = [[NSArray alloc] initWithObjects:values count:2]; NSLog(@"%@",array ]);
Output
2014-01-27 19:53:51.555 array[25037] ( Eezy , Tutorials )
Querying an Array
- containsObject:
Returns a Boolean value that indicates whether a given object is present in the array.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials"]; BOOL containsObject = [array containsObject:@"Eezy"]; NSLog(@"Contains Object Eezy: ",containsObject) containsObject = [array containsObject:@"Eezy Tutorials"]; NSLog(@"Contains Object Eezy Tutorials: ",containsObject)
Output
2014-01-27 19:53:51.555 array[25037] Contains Object Eezy: 1 2014-01-27 19:53:51.558 array[25037] Contains Object Eezy: 0
- count
Returns the number of objects currently in the array.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials"]; NSLog(@"Count: %d",[array count]);
Output
2014-01-27 19:53:51.555 array[25037] Count: 2
- getObjects:range:
Copies the objects contained in the array that fall within the specified range to aBuffer.
Example
NSArray *tempArray = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; id *array; NSRange range = NSMakeRange(1, 2); objects = malloc(sizeof(id) * range.length); [tempArray getObjects:array range:range]; for (index = 0; index < range.length; index++) { NSLog(@"Array object index %d: %@",index, objects[index]); }
Output
2014-01-27 19:53:51.555 array[25037] Array object index 0: Tutorials 2014-01-27 19:53:51.555 array[25037] Array object index 0: Website
- firstObject
Returns the first object in the array.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; NSLog(@"First Object: %@", [array firstObject])
Output
2014-01-27 19:53:51.555 array[25037] First Object: Eezy
- lastObject
Returns the last object in the array.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; NSLog(@"Last Object: %@", [array lastObject])
Output
2014-01-27 19:53:51.555 array[25037] Last Object: Website
- objectAtIndex:
Returns the object located at the specified index.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; NSLog(@"Object at index 2: %@", [array objectAtIndex:2])
Output
2014-01-27 19:53:51.555 array[25037] Object at index 2: Website
- objectAtIndexedSubscript:
Returns the object at the specified index. If index is beyond the end of the array (that is, if index is greater than or equal to the value returned by count), an NSRangeException is raised.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; NSLog(@"Object at Indexed Subscript 2: %@", [array objectAtIndexedSubscript:2]) NSLog(@"Object at Indexed Subscript 3: %@", [array objectAtIndexedSubscript:3])
Output
2014-01-27 19:53:51.555 array[25037] Object at Indexed Subscript 2: Website 2014-01-27 19:53:51.555 array[25037] Object at Indexed Subscript 3: NSRangeException: out of bounds
- objectsAtIndexes:
Returns an array containing the objects in the array at the indexes specified by a given index set.
Example
NSArray *tempArray = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; array = [tempArray objectsAtIndexes:[NSIndexSet indexSetWithIndexesInRange:NSMakeRange(1, 2)]]; NSLog(@"%@",array ]);
Output
2014-01-27 19:53:51.555 array[25037] ( Tutorials , Website )
- objectEnumerator
Returns an enumerator object that lets you access each object in the array. You can use Fast enumeration that is for in statement instead
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; NSEnumerator *enumerator = [array objectEnumerator]; id anObject; while (anObject = [enumerator nextObject]) { NSLog(@"%@",anObject ]); }
Output
2014-01-27 19:53:51.555 array[25037] Eezy 2014-01-27 19:53:51.555 array[25037] Tutorials 2014-01-27 19:53:51.555 array[25037] Website
- reverseObjectEnumerator
Returns an enumerator object that lets you access each object in the array, in reverse order.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; NSEnumerator *enumerator = [array reverseObjectEnumerator]; id anObject; while (anObject = [enumerator nextObject]) { NSLog(@"%@",anObject ]); }
Output
2014-01-27 19:53:51.555 array[25037] Website 2014-01-27 19:53:51.555 array[25037] Tutorials 2014-01-27 19:53:51.555 array[25037] Eezy
Finding Objects in an Array
- indexOfObject:
Returns the lowest index whose corresponding array value is equal to a given object. If none of the objects in the array is identical to anObject, returns NSNotFound.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; NSLog(@"Index of Website is %d",[array indexOfObject:@"Website"]);
Output
2014-01-27 19:53:51.555 array[25037] Index of Website is 2
- indexOfObject:inRange:
Returns the lowest index within a specified range whose corresponding array value is equal to a given object. If none of the objects in the array is identical to anObject, returns NSNotFound.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website",@"Eezy"]; NSRange range = NSMakeRange(1, 2); NSLog(@"Index of Eezy in range 1,3 is %d",[array indexOfObject:@"Eezy" inRange:range ]); range = NSMakeRange(0, 3); NSLog(@"Index of Eezy in range 0,3 is %d",[array indexOfObject:@"Eezy" inRange:range ]);
Output
2014-01-27 19:53:51.555 array[25037] Index of Eezy in range 1,3 is 3 2014-01-27 19:53:51.555 array[25037] Index of Eezy in range 0,3 is 0
- indexOfObjectIdenticalTo:
Returns the lowest index whose corresponding array value is identical to a given object. It checks for same memory address. This is used to compare two array sharing some objects.If none of the objects in the array is identical to anObject, returns NSNotFound.
Example
NSString *str = @"Eezy"; NSArray *array = [NSArray arrayWithObjects:str,@"Tutorials", @"Website"]; NSLog(@"Index of Eezy identical",[array indexOfObject:str]); NSLog(@"Index of Eezy identical",[array indexOfObject:@"Eezy"]);
Output
2014-01-27 19:53:51.555 array[25037] Index of Eezy identical is 3 2014-01-27 19:53:51.555 array[25037] Index of Eezy identical is 2147483647 // NSNotFound
- indexOfObjectIdenticalTo:inRange:
Returns the lowest index within a specified range whose corresponding array value is equal to a given object .
Example
NSString *str = @"Eezy"; NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website", str]; NSRange range = NSMakeRange(1, 3); NSLog(@"Index of Eezy identical",[array indexOfObject:str inRange:range ]); range = NSMakeRange(0, 3); NSLog(@"Index of Eezy identical",[array indexOfObject:str inRange:range ]);
Output
2014-01-27 19:53:51.555 array[25037] Index of Eezy identical in range 1,3 is 3 2014-01-27 19:53:51.555 array[25037] Index of Eezy identical in range 0,3 is 2147483647 // NSNotFound
- indexOfObjectPassingTest:
Returns the index of the first object in the array that passes a test in a given Block.
Example
NSArray *array = [NSArray arrayWithObjects:@"Eezy",@"Tutorials", @"Website"]; int index = [array indexOfObjectPassingTest:^BOOL(id element,NSUInteger idx,BOOL *stop){ return [(NSArray *)element containsObject:@"Eezy"]; }]; NSLog(@"Index is %d",index); if (index >= 0 && index < [arrayWithArray count]){ [arrayWithArray removeObjectAtIndex:index]; }
Output
2014-01-27 19:53:51.555 array[25037] Index is 0
Advertisements