OC Array
Nsarray is a static array, that is, it points to the content is immutable, it points to a memory area, once initialized, it can not be used to modify the memory area of the data, but it is able to read the data.
Nsmutablearray is a subclass of Nsarray that can make changes to the contents of the memory area that it points to, and can increase the array contents
The subscript for the first data for Nsarray and Nsmutablearray is 0.
1, nsarray non-variable group
[Array1 Count]: The length of the array.
// Create OC array object
NSArray * array = [NSArray arrayWithObject: @ "jack"];
// nil is the end of the array element
NSArray * array1 = [NSArray arrayWithObjects: @ "jack", @ "haha", nil];
NSLog (@ "% ld", array1.count); // print the length of the array
[Array1 objectatindex:0]: Accessing elements in an array
ARRAY1[0];
NSArray * array1 = [NSArray arrayWithObjects: @ "jack", @ "haha", nil];
// access the output array element
NSLog (@ "% @", [array1 objectAtIndex: 1]);
NSLog (@ "% @", array1 [0]);
To create an OC array object quickly:
[Nsarray arraywithobjects;@ "Jack" @ "haha" ..., nil]: Initialize the assignment to the array object. You can write pointers to arbitrary objects, and you must use nil at the end.
// Quickly create NSArray array object
NSArray * array = @ [@ "jack", @ "rose‘, @ "haha"];
NSLog (@ "% @", array1);
Fast traversal of nsarray arrays
NSArray * array = @ [@ "jack", @ "haha"];
// id obj represents every element in the array
for (id obj in array) {
NSLog (@ "% @", obj);
}
Find the position of an element in an array
NSArray * array = @ [@ "jack", @ "haha"];
// id obj represents every element in the array
for (id obj in array) {
// find the position of the obj element in the array
NSUInteger i = [array indexOfObject: obj];
NSLog (@ "% ld-% @", i, obj);
}
Use block
NSArray * array = @ [@ "jack", @ "haha"];
// Every time an element is traversed, block is called once
// And the current element and index position are passed to the block as a reference
[array enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL * stop) {
NSLog (@ "---------");
}];
/ *
Bool * stop
If (idx == 0) {
Stop traversing
* Stop = YES;
}
* /
OBJECTIVE-C (Foundation framework an array (Nsarray))