Nsarray:
Only OC objects can be loaded, and null values cannot be loaded. null indicates the end of array elements.
// Create an empty array
Nsarray * array = [nsarray array];
// Create an array with an element
Array = [nsarray arraywithobject: @ "123"];
// Create an array with Multiple Elements
Array = [nsarray arraywithobjects: @ "A", @ "B", nil]; // The NIL null value cannot be loaded. The null value indicates that the array element ends.
// Obtain the number of elements
Int COUNT = [array count]; // same as Count = array. Count;, get method is called.
// Whether it contains an element
-(Bool) containsobject :( ID) anobject;
Returns the last element.
-(ID) lasobject;
// Obtain the element at the index position
-(ID) objectatindex :( nsuinteger) index;
// Obtain the element position
-(Nsuinteger) indexofobject :( ID) anobject;
// Locate the element in the range
-(Nsuinteger) indexofobject :( ID) anobject inrange :( nsange) range;
// Compare whether the content of the two sets is the same
-(Bool) isw.toarray :( nsarray *) otherarray;
// Returns the first identical object element in two sets.
-(ID) firstobjectcommonwitharray :( nsarray *) otherarray;
// Let all the elements in the Set execute the aselector method.
-(Void) makeobjectsperformselector :( SEL) aselector;
// Let all elements in the Set execute the aselector method, add parameters to this method, but only one parameter is supported
-(Void) makeobjectsperformselector :( SEL) aselector withobject :( ID) argument
Nsarray:
// Add an element and return a new array (the method caller has not changed)
-(Nsarray *) arraybyaddingobject :( ID) anobject
// Add all the elements of otherarray and return a new nsarray (the method itself has not changed)
-(Nsarray *) arraybyaddingobjectsfromarray :( nsarray *) otherarray;
// Truncate the range array
-(Nsarray *) subarraywithrange :( nsenge) range;
// Use separator as the concatenation character and splice it into a string
-(Nsstring *) componentsjoinedbystring :( nsstring *) Separator
// Persists nsarray to a file
-(Bool) writetofile :( nsstring *) path atomically :( bool) useauxiliaryfile
Array Memory Management;
If you place an object in an array, add one to the counter of the object. That is to say, the array will perform a retain operation on him.
When an array is destroyed ([array ralases]), all the internal elements are release.
The Code is as follows:
Void *: D indicates a pointer to any type.
Array traversal:
Method 1: normal Traversal
Method 2: Fast Traversal
Method 3: Use block Traversal
Method 4: Use the iterator
Nsenumerator iterator:
Set iterator, which can be used to traverse collection Elements
Nsarray has corresponding methods to obtain the iterator
// Obtain an iterator for positive traversal.
-(Nsenumerator *) objectenumerator;
// Obtain an iterator for reverse traversal.
-(Nsenumerator *) reverseobjectenumerator;
Common nsenumerator methods:
// Obtain the next element
-(ID) nextobject;
// Obtain all elements
-(Nsarray *) allobjects
The Demo code is as follows:
Student. h file content:
# Import <Foundation/Foundation. h>
@ Interface Student: nsobject
+ (ID) student;
-(Void) test;
-(Void) Test2 :( nsstring *) STR;
@ End
Student. M file content:
# Import "student. H"
@ Implementation student
+ (ID) Student {
Return [[[STUDENT alloc] init] autorelease];
}
-(Void) test {
Nslog (@ "% @-> test", self );
}
-(Void) Test2 :( nsstring *) STR {
Nslog (@ "% @-> Test2 % @", self, STR );
}
-(Void) dealloc {
Nslog (@ "% @ is destoryed", self );
[Super dealloc];
}
@ End
Void arraycreate (){
// Create a null Array
// Nsarray * array = [nsarray array];
}
Void arrayuse (){
Nsobject * OBJ = [[nsobject alloc] init];
Nslog (@ "OBJ: % @", OBJ );
Nsarray * array = [nsarray arraywithobjects: @ "A", @ "B", @ "C", OBJ, nil];
Nsstring * Last = [array lastobject];
Nslog (@ "last: % @", last );
[OBJ release];
}
# Memory Management of The Pragma mark Memory Manager Array
Void arraymemory (){
Student * stu1 = [[STUDENT alloc] init];
Student * stu2 = [[STUDENT alloc] init];
Student * stu3 = [[STUDENT alloc] init];
Nslog (@ "stu1: % zi", [stu1 retaincount]); // 1
Nsarray * array = [[nsarray alloc] initwithobjects: stu1, stu2, stu3, nil];
Nslog (@ "stu1: % zi", [stu1 retaincount]); // 2
Nslog (@ "array count: % zi", [array count]);
[Array release];
[Stu1 release];
[Stu2 release];
[Stu3 release];
}
# Pragma mark sends messages to elements in the array
Void arraymessage (){
Student * stu1 = [Student];
Student * stu2 = [Student];
Student * stu3 = [Student];
Nsarray * array = [[nsarray alloc] initwithobjects: stu1, stu2, stu3, nil];
// [Array makeobjectsperformselector: @ selector (TEST)];
// There are parameters, but only one of them can be
[Array makeobjectsperformselector: @ selector (Test2 :) withobject: @ "123"];
[Array release];
}
# Pragma mark normal Traversal
Void arrayfor1 (){
Nsarray * array = [nsarray arraywithobjects: @ "1", @ "2", @ "3", nil];
Int COUNT = array. count;
For (INT I = 0; I <count; I ++ ){
Id OBJ = [array objectatindex: I];
Nslog (@ "% I-% @", I, OBJ );
}
}
# Pragma mark fast Traversal
Void arrayfor2 (){
Nsarray * array = [nsarray arraywithobjects: @ "1", @ "2", @ "3", nil];
Int COUNT = array. count;
Int I = 0;
For (id obj in array ){
Nslog (@ "% I-% @", I, OBJ );
I ++;
}
}
# Pragma mark uses block Traversal
Void arrayfor3 (){
Nsarray * array = [nsarray arraywithobjects: @ "1", @ "2", @ "3", nil];
[Array enumerateobjectsusingblock: ^ (id obj, nsuinteger idx, bool * Stop ){
Nslog (@ "% Zi-> % @", idx, OBJ );
// * Stop = yes; // change the bool outside and terminate the traversal.
}];
}
# Pragma mark uses iterator
Void arrayfor4 (){
Nsarray * array = [nsarray arraywithobjects: @ "1", @ "2", @ "3", nil];
Nsenumerator * enumerator = [array objectenumerator];
// If it is placed after the traversal, It is null because the traversal is complete and no value is available.
Nsarray * array2 = [enumerator allobjects];
Nslog (@ "array2: % @", array2 );
// Obtain the next element to be traversed
Id OBJ = nil;
While (OBJ = [enumerator nextobject]) {
Nslog (@ "OBJ = % @", OBJ );
}
}
Int main (INT argc, const char * argv [])
{
@ Autoreleasepool {
Nslog (@ "------ array create --------");
Arraycreate ();
Nslog (@ "------ array use -----------");
Arrayuse ();
Nslog (@ "------ Array Memory Manager ----");
Arraymemory ();
Nslog (@ "------ array message ----------");
Arraymessage ();
Nslog (@ "------ array ---------");
Arrayfor1 ();
Nslog (@ "------ array for2 --------");
Arrayfor2 ();
Nslog (@ "------ array for3 ---------");
Arrayfor3 ();
Nslog (@ "------ array for4 ---------");
Arrayfor4 ();
}
Return 0;
}