==============================Nsarray==================================( Immutable array, once created, cannot be changed)
Ordered collection of objects. Immutable (you cannot add or remove objects to it once it's created)
Important methods:
+ (ID) arraywithobjects :( ID) firstobject,...; // nil terminated
-(INT) count; // obtain the number of objects in the array.
-(ID) objectatindex :( INT) index; // obtain the object whose index is I.
-(Bool) containsobject :( ID) anobject; // when anobject appears in array, yes is returned (actually determined by isequal: method)
-(Unsigned) indexofobject :( ID) anobject; // search for anobject in array and return its minimum index value. Nsnotfound not found.
-(Void) makeobjectsperformselector :( SEL) aselector;
-(Nsarray *) sortedarrayusingselector :( SEL) aselector;
-(ID) lastobject; // obtain the last object in array. If no object exists in array, Nil is returned.
Note:
Class Method arraywithobjects can create an autoreleased nsarray of the items. For example:
@ Implementation myobject
-(Nsarray *) coolcats {
Return [nsarray arraywithobjects: @ "Steve", @ "Ankush", @ "Sean", nil];
}
@ End
Other convenient create with methods (all return autoreleased objects ):
[Nsstring stringwithformat: @ "Meaning of % @ is % d", @ "life", 42];
[Nsdictionary dictionarywithobjectsandkeys: Ankush, @ "ta", janestudent, @ "student", nil];
[Nsarray arraywithcontentsoffile :( nsstring *) path];
-----Create an array-----
Nsarray * array = [[nsarray alloc] initwithobjects: @ "one", @ "two", @ "three", @ "four", nil];
Self. dataarray = array;
Nslog (@ "self. dataarray count is: % d", [self. dataarray count]);
Nslog (@ "self. dataarray index 2 is: % @", [self. dataarray objectatindex: 2]);
------Copy data from one array to another (variable level)-------
// 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 );
// Copy
// Id obj;
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 );
// Quick 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 );
// 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 );
==============================Nsmutablearray ================================= (Variable array)
Mutable version of nsarray.
-(Void) addobject :( ID) anobject; // Add anobject at the end of array. Adding NIL is invalid.
-(Void) addobjectsfromarray :( nsarray *) otherarray; // Add the objects in otherarray in sequence at the end of array.
-(Void) insertobject :( ID) anobject atindex :( INT) index; // insert anobject to the index. If the index is occupied, the subsequent object will be moved to the backend.
-(Void) removeobjectatindex :( INT) index; // Delete the object at the index. The objects following the index are moved forward in sequence.
-(Void) removeobject :( ID) anobject; // delete all objects equal to the anobject and use isequal: as the comparison method.
-(Void) removeallobjects;
Note:We cannot add nil to array. But sometimes we really want to add an empty object to array. We can use nsnull to do this. For example:
[Myarray addobject: [nsnull null];
-----Allocate capacity to Arrays-----
// Nsarray * array;
Array = [nsmutablearray arraywithcapacity: 20];
-----Add an object to the end of the array-----
//-(Void) addobject: (ID) anobject;
// Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
[Array addobject: @ "four"];
Nslog (@ "array: % @", array );
-----Delete the object at the specified index in the array-----
//-(Void) removeobjectatindex: (unsigned) index;
// Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
[Array removeobjectatindex: 1];
Nslog (@ "array: % @", array );
-----Array Enumeration-----
// 1.-(nsenumerator *) objectenumerator; // forward and backward
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
Nsenumerator * enumerator;
Enumerator = [array objectenumerator];
Id thingie;
While (thingie = [enumerator nextobject]) {
Nslog (@ "thingie: % @", thingie );
}
// 2,-(nsenumerator *) reverseobjectenumerator; // forward from the back
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
Nsenumerator * enumerator;
Enumerator = [array reverseobjectenumerator];
Id object;
While (Object = [enumerator nextobject]) {
Nslog (@ "Object: % @", object );
}
// 3. Fast Enumeration
Nsmutablearray * array = [nsmutablearray arraywithobjects: @ "one", @ "two", @ "three", nil];
For (nsstring * string in array ){
Nslog (@ "string: % @", string );
}
-----Nsvalue (packaging any object)-----
// Put nsrect into nsarray
Nsmutablearray * array = [[nsmutablearray alloc] init];
Nsvalue * value;
Cgrect rect = cgrectmake (0, 0,320,480 );
Value = [nsvalue valuewithbytes: & rect objctype: @ encode (cgrect)];
[Array addobject: value];
Nslog (@ "array: % @", array );
// Extract from Array
Value = [array objectatindex: 0];
[Value getvalue: & rect];
Nslog (@ "value: % @", value );
// When array replacement is also performed
[Objectsarray replaceobjectatindex: 0 withobject: P2];
// Execute clearing the Array
[Objectsarray removeallobjects];
Nsarray and nsmutablearray in the foundation framework