Objective-C Tutorial: NSArray
Example:
NSArray * foo = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil];
Factory methods are static methods that build new instances of NSArrays from given parameters and return them.
The table below details on all of the factory methods for the NSArray class.
| + (id)array |
Creates and returns an empty array |
| + (id)arrayWithArray:(NSArray *)anArray |
Creates and returns an array containing the objects in another given array. |
| + (id)arrayWithContentsOfFile:(NSString *)aPath |
Creates and returns an array containing the contents of the file specified by a given path. * The file must be of type .plist for this method to work |
| + (id)arrayWithContentsOfURL:(NSURL *)aURL |
Similar to arrayWithContentsOfFile except it will load the .plist remotely from a given website. This would be a very simple way to get data from a web service. |
| + (id)arrayWithObject:(id)anObject |
Creates and returns an array containing a given object. This will just be a 1 element array |
| + (id)arrayWithObjects:(id)firstObj, … |
This method is used when you have multiple objects on hand and want easily insert them into an array. Make sure the last element you add is nil or this method won’t work. |
| + (id)arrayWithObjects:(const id *)objects count:(NSUInteger)count |
Creates and returns an array that includes a given number of objects from a given C array. |
|
// I am using strings, but you can add just about any object to an NSArray
// Creates an NSArray with one object
NSArray * myArray = [NSArray arrayWithObject:@"foo"];
// Creates an NSArray with multiple objects. Don't forget to add nil as the last object
NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];
// Creates an NSArray from another NSArray
NSArray * myArray3 = [NSArray arrayWithArray:myArray2];
// This will create an NSArray from data on iCodeBlog. Go ahead and try it out, this file exists on our servers and contains valid data.
NSArray * myArray4 = [NSArray arrayWithContentsOfURL:[NSURL URLWithString:@"http://icodeblog.com/wp-content/uploads/2009/08/foo.plist"]];
|
You can also choose not to use factory methods and just use the normal NSArray initializers.
They are pretty much the same as the factory methods only you do the allocation yourself. An example of this might be:
NSArray * foo = [[NSArray alloc] initWithObjects:@"foo",@"bar",@"baz",nil];
Accessing Objects in a NSArray
To access an object in an NSArray, you use the -objectAtIndex: method, as in the following example:
NSArray *numbers;
NSString *string;
numbers = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
string = [numbers objectAtIndex: 2]; // @"Three"
Of course, you have to be careful not to ask for an object at an index which is negative or
bigger than the size of the array; if you do, an NSRangeException is raised
(we'll learn more about exceptions in another tutorial).
To get the length of an array, you use the method -count, as in:
NSArray *numbers;
int i;
numbers = [NSArray arrayWithObjects: @"One", @"Two", @"Three", nil];
i = [numbers count]; // 3
Searching The Array:
|
NSString * f = @"foo";
NSString * b = @"bar";
NSString * z = @"baz";
NSArray * myArray2 = [NSArray arrayWithObjects:f,b,z,nil];
NSInteger idx = [myArray2 indexOfObject:b];
// This would return 1 (since NSArrays are 0 - indexed)
|
Sorting Arrays:
NSArray *sortedArray =
[myArray2 sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
// This will return a sorted array that looks like [@"bar",@"baz",@"foo"]
Looping Through Arrays:
for(NSString * myStr in myArray2) {
NSLog(myStr);
}
Saving Arrays For Later:
NSArray * myArray2 = [NSArray arrayWithObjects:@"foo",@"bar",@"baz",nil];
[myArray2 writeToFile:filePath atomically:YES];
Reference url:
http://icodeblog.com/2009/08/26/objective-c-tutorial-nsarray/
http://adeem.me/blog/2009/05/19/iphone-programming-tutorial-part-1-uitableview-using-nsarray/
http://memo.tv/nsarray_vs_c_array_performance_comparison