An array of objective-c is more powerful than an array of C++,java, and Nsarray saved objects can be different objects. But can only save objects, int, char,double and other basic data types can not be saved directly, you need to convert to an object to join the array.
1, nsarray non-variable group
[Array Count]: the length of the array.
[Array Objectatindex 0]: The ID of the incoming array pin gets the data object.
[Arraywithobjects; ...] : Initializes an assignment to an array object. You can write pointers to arbitrary objects, and you must use nil at the end.
1.1. Creating an array
1. Creating an array
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", @ "four", nil]; |
|
2. The number of objects contained in the array
3. Gets the object at the specified index
[Self.dataarray Objectatindex:2] |
|
4. Copying data from one array to another array (variable-number)
Arraywitharray: Nsarray *array1 = [[Nsarray alloc] init]; Nsmutablearray *mutablearray = [[Nsmutablearray alloc] init]; Nsarray *array = [Nsarray arraywithobjects: @ "A", @ "B", @ "C", nil]; NSLog (@ "array:%@", array); Mutablearray = [Nsmutablearray Arraywitharray:array]; NSLog (@ "mutablearray:%@", Mutablearray);
Array1 = [Nsarray Arraywitharray:array]; NSLog (@ "array1:%@", array1); |
|
5. COPY
nsmutablearray *newarray = [[Nsmutablearray alloc] init]; nsarray *oldarray = [Nsarray arraywithobjects: @ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "G", @ "H", nil]; nslog (@ "oldarray:%@", Oldarray); for (int i = 0; i < [Oldarray count]; i++) { obj = [[OldArray objectAtIndex: I] copy]; [newarray Addobject:obj]; } // nslog (@ "newArray:%@", NewArray); [newarray release]; |
|
6. Deep copy
Nsmutablearray *newarray = [[Nsmutablearray alloc] init]; Nsarray *oldarray = [Nsarray arraywithobjects: @ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "G", @ "H", nil]; NSLog (@ "oldarray:%@", Oldarray); NewArray = (nsmutablearray*) cfpropertylistcreatedeepcopy (Kcfallocatordefault, (cfpropertylistref) OldArray, Kcfpropertylistmutablecontainers); NSLog (@ "newarray:%@", NewArray); [NewArray release]; |
|
7. Fast Enumeration
Nsmutablearray *newarray = [[Nsmutablearray alloc] init]; Nsarray *oldarray = [Nsarray arraywithobjects: @ "A", @ "B", @ "C", @ "D", @ "E", @ "F", @ "G", @ "H", nil]; NSLog (@ "oldarray:%@", Oldarray);
for (id obj in Oldarray) { [NewArray Addobject:obj]; } // NSLog (@ "newarray:%@", NewArray); [NewArray release]; |
|
8, cut the fractional group
NSString *string = [[NSString alloc] initwithstring:@ "One,two,three,four"]; NSLog (@ "string:%@", string); Nsarray *array = [string componentsseparatedbystring:@ ","]; NSLog (@ "array:%@", array); [String release]; |
|
9. Merging elements from arrays to strings
Nsarray *array = [[Nsarray alloc] initwithobjects:@ "One", @ "one", @ "three", @ "four", nil]; NSString *string = [Array componentsjoinedbystring:@ ","]; NSLog (@ "string:%@", string); |
|
10. Allocating capacity to arrays
Array = [Nsmutablearray arraywithcapacity:20]; |
|
11. Adding objects at the end of the array
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil]; [Array addobject:@ "four"]; NSLog (@ "array:%@", array); |
|
12. Delete the object at the specified index in the array
Nsmutablearray *array = [Nsmutablearray arraywithobjects:@ "one", @ "one", @ "three", nil]; [Array removeobjectatindex:1]; NSLog (@ "array:%@", array); |
The Nsarray and Nsmutablearr of Objective-c grammar