Store objects in an array in some order
Arrays store objects in an ordered sequence. Therefore, when the order of a set of objects is important, the array is used. For example, many applications use arrays to provide content to items in a row or menu in a tabular view, objects indexed to 0 correspond to the first row, objects indexed to 1 correspond to the second row, and so on. The time to access an object in an array is longer than the time it takes to access an object in the collection.
Create an array
NSArray
The class provides many initializer and class factory methods for creating arrays and initializing array, but there are several methods that are especially common and useful. You can use the arrayWithObjects:count:
and arrayWithObjects:
methods (and their corresponding initializers initWithObjects:count:
and initWithObjects:
) to create arrays from a series of objects. When you use the previous method, the second parameter specifies the number of objects in the first parameter (a static C array), and the latter is a comma-delimited sequence of objects (to nil
terminate).
Compose a static array of String objects |
NSString *objs[3] = {@ "one", @ "one", @ "three"}; |
Create an array object with the static array |
Nsarray *arrayone = [Nsarray arraywithobjects:objs count:3]; |
Create an array with a nil-terminated list of objects |
Nsarray *arraytwo = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", nil]; |
When creating a mutable array, you can use the arrayWithCapacity:
(or initWithCapacity:
) method to create an array. The capacity parameter provides the class with hints about the expected size of the array, making the array more efficient at run time. The array can even exceed the specified capacity.
You can also use container literal constants ... @[
]
create an array where the items between the square brackets are comma-delimited objects. For example, to create an array that contains a string, a number, and a date, you can write the following code:
Nsarray *myarray = @[@ "Hello World", @67, [NSDate Date]]; |
Accessing objects in an array
Typically, you call objectAtIndex:
a method to access an object in an array by specifying the index position of the object in the array (starting at 0).
NSString *thestring = [Arraytwo objectatindex:1]; Returns second object in array |
NSArray
Provides other ways to access an object in an array or its index. For example, there is lastObject
, firstObjectCommonWithArray:
and indexOfObjectPassingTest:
.
You can access the objects in the array using the subscript notation instead NSArray
of the method you use. For example, to access the myArray
second object (created above), you can write the following code:
ID theobject = myarray[1]; |
Another common task associated with arrays is to perform some action on each object in an array-this is the process called enumeration. You typically enumerate arrays to determine whether an object or multiple objects match a value or condition, and if an object matches, use that object to complete an operation. You can enumerate arrays in one of three ways: quickly enumerate, use block enumerations, or use NSEnumerator
objects. As the name implies, fast enumeration is often faster than using other techniques to access objects in an array. Fast enumeration is a language feature that requires a specific syntax:
for (
type variable in
)
{
Array /* inspect
variable, do something with it */ }
For example:
Nsarray *myarray =//Get array |
For (NSString *cityname in MyArray) { |
if ([CityName isequaltostring:@ "Cupertino"]) { |
NSLog (@ "We ' re near the mothership!"); |
Break |
} |
} |
Several NSArray
methods use blocks to enumerate arrays, the simplest of which is enumerateObjectsUsingBlock:
. This block has three parameters: the current object, its index, and the Boolean value of the reference (set to YES
terminate enumeration). The code in this block performs exactly the same as the code inside the curly braces in the Quick enumeration statement.
Nsarray *myarray =//Get array |
[MyArray enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) { |
if ([obj isequal:@ "Cupertino"]) { |
NSLog (@ "We ' re near the mothership!"); |
*stop = YES; |
} |
}]; |
Managing variable Arrays
NSArray
There are other methods for sorting the array, searching the array, and calling the method on each object in the array.
addObject:
You can add an object to a mutable array by calling the method, and the object is placed at the end of the array. You can also use insertObject:atIndex:
to place objects in a variable array at a specific location. removeObject:
removeObjectAtIndex:
You can remove an object from a mutable array by calling a method or method.
You can also use the following marker number to insert an object into a variable array at a specific location.
Nsmutablearray *mymutablearray = [Nsmutablearray arraywithcapacity:1]; |
NSDate *today = [NSDate Date]; |
Mymutablearray[0] = today; |
Use of arrays