NsarrayUsed to store an ordered list of objects, which are immutable and cannot store basic data types in C, such as int, float, enum, struct, or nil
Nsarray *array=[nsarray arraywithobject:@ "12345"];
after the array is created, it is immutable and needs to be initialized at the time of creation.
when an object is plugged into an array, the object's counter is incremented by 1.
Nsarray Traversal
Nsuinteger count = [array count]; for (int i=0;i<count;i++) {id obj = [array objectatindex:i]; }
for (ID obj in array)
Nsarray *array=[nsarray arraywithobjects:@ "123", @ "456"]; // gets the iterator for the array, the positive sequence Nsenumerator @eunm =[array objectenumerator]; // gets the next element to traverse ID obj =nil; while (obj=[enum Nextobject]) { NSLog (@ "obj=%@", obj);}
Reverse iterator: Nsenumerator *enum=[array reverseobjectenumerator];
[Array enumerateobjectsusingblock:^ (ID object,nsuinteger index,bool *stop) {NSLog (@ "%@-%zi", Object,index); }]
- Iterator traversal: Gets all the elements in the array nsarray *array = [enumerator allobjects];
AllObjects is to remove objects that have not been traversed
Nsarray derivation of a new collection
- -(Nsarray *) Arraybyaddingobject: (ID) anobject
Add an element that returns a new Nsarray (the method caller itself has not changed)
- -(Nsarray *) Arraybyaddingobjectsfromarray: (Nsarray *) Otherarray
Add all elements of Otherarray and return a new Nsarray (the method caller itself has not changed)
- -(Nsarray *) Subarraywithrange: (nsrange) Range
Array elements that intercept range ranges
Nsarray Other methods
- -(NSString *) componentsjoinedbystring: (NSString *) separator
Concatenation of array elements into a string using separator as a concatenation character
- -(BOOL) WriteToFile: (NSString *) path atomically: (BOOL) Useauxiliaryfile
Persist a nsarray to a file
If you want to read the contents of a file into an array, the contents of the file must conform to a certain format
nsmutablearray (variable group)
- A variable Nsarray,nsarray subclass that can optionally add or remove elements
- Ways to create Nsmutablearray
Nsmutablearray *array=[nsmutablearray arraywithobject:@ "1"]; // adding elements [Array addobject:@ "2"]; // Delete Element [Array removeobject:@ "1"]; // Delete last element [Array Removelastobject] // Delete all elements [Array removeallobjects];
You can also use the Create Nsarray method to create a Nsmutablearray
- When an element is added to a collection, a retain operation is performed, and when an element is removed from the collection, a release operation is performed, and when the collection is destroyed (Dealloc is called), All elements in the collection perform a release operation once (this principle also applies to other collections: Nsictionary\nsset, etc.)
Sorting method Reference Nsarray Sort, sort the method that is called by the return value of a variable group, using a method with no return value, using a method with a return value for an immutable group
oc--foundation-Common Classes (2)----Nsarray