Nsarray Basic Concepts
What is Nsarray
Nsarray is an array class in OC, and in development it is recommended to use Nsarray instead of arrays in the C language.
What are the use notes of Nsarray
Only any OC objects can be stored, and are ordered
Cannot store non-OC objects, such as int\float\double\char\enum\struct, etc.
It is immutable, and once initialized, the contents of it are always fixed, the elements inside cannot be deleted, and no elements can be added to it.
Nsarray is printed with NSLog (), and the output is in the form of parentheses.
Nil is not stored in Nsarray because Nsarray considers nil to be the end of the array (nil is the tag at the end of the array element). Nil is 0. 0 is also the basic data type and cannot be stored in the Nsarray.
What are the common methods of Nsarray?
-(Nsuinteger) count;
Get the number of collection elements
-(ID) Objectatindex: (Nsuinteger) index;
Gets the element of the index position
-(BOOL) Containsobject: (ID) anobject;
Whether to include an element
-(ID) lastobject;
Returns the last element
-(ID) firstobject;
Returns the last element
-(Nsuinteger) Indexofobject: (ID) anobject;
Finds the position of the AnObject element in the array (returns-1 if not found)
-(Nsuinteger) Indexofobject: (ID) anobject inrange: (nsrange) range;
Find the position of the AnObject element in the array within range range
Writing Nsarray Shorthand form
Nsarray *arr = [Nsarray arraywithobjects:@ "LJ", @ "LM", @ "JJJ", nil];
Nsarray *arr = @[@ "Lnj", @ "LM", @ "JJJ"];
Gets the shorthand for the array element
NSLog (@ "%@", [arr objectatindex:0]);
NSLog (@ "%@", arr[0]);
Nsarray traversal
Enhancing the use of a For loop
How to use the enhanced for loop to traverse the Nsarray array
Remove elements from arr one by one and assign the extracted elements to obj
Note: The type of obj can be written according to the type of elements in the array, not necessarily written nsobject
For (NSString *obj in arr) {
NSLog (@ "obj =%@", obj);
}
How to iterate through an array using an iterator to an OC array
Each time an element is taken, the block is called.
Each call to block will pass to us the index of the element and element that is currently taken out.
Obj is the currently removed element, and IDX is the index of the current element.
Stop is used to control when to stop traversing
[arr enumerateobjectsusingblock:^ (id obj, Nsuinteger idx, BOOL *stop) {
if (idx = = 1) {
*stop = YES;
}
NSLog (@ "obj =%@, idx =%lu", obj, idx);
}];
To all object method messages in the Nsarray
How do I send a message to all the objects in the array?
For:
Person *P1 = [person new];
Person *P2 = [person new];
Person *P3 = [person new];
Person *P4 = [person new];
Nsarray *arr = @[p1, p2, p3, P4];
Method One:
[Arr enumerateobjectsusingblock:^ (person *obj, Nsuinteger idx, BOOL *stop) {
[obj say];
}];
2. Method Two:
If you use the OC array to store the object, you can call the OC array method to have all elements in the array execute the specified method
Note: If the array does not hold the same type of data, and there is no same method, then an error will be
[Arr makeobjectsperformselector: @selector (say)];
Withobject: Arguments that need to be passed to the calling method
[Arr makeobjectsperformselector: @selector (saywithname:) withobject:@ "WC"];
Array sorting
How to sort the data
Nsarray *arr = @[@10, @20, @5, @7, @15];
NSLog (@ "sort before:%@", arr);
Note: To use the Compare method to sort elements in a group, the elements in the array must be objects in the foundation framework, that is, they cannot be custom objects
Nsarray *newarr = [arr sortedarrayusingselector: @selector (compare:)];
NSLog (@ "After sorting:%@", NEWARR);
To sort an object
Person *P1 = [person new];
P1.age = 10;
Person *P2 = [person new];
P2.age = 20;
Person *P3 = [person new];
P3.age = 5;
Person *P4 = [person new];
P4.age = 7;
Nsarray *arr = @[p1, p2, p3, P4];
NSLog (@ "sort before:%@", arr);
Sort by age of person
The method is sorted by default in ascending order
Nsarray *newarr = [arr sortedarraywithoptions:nssortstable usingcomparator:^nscomparisonresult (person *obj1, person * OBJ2) {
Each time the block is called, the two elements in the array are taken out to us
Two points
NSLog (@ "obj1 =%@, Obj2 =%@", obj1, OBJ2);
return obj1.age > Obj2.age; Descending
return Obj1.age < Obj2.age; Ascending
/*
The method of modification, which produces random numbers
if (Obj1.age > Obj2.age) {
5 4
return nsordereddescending;
}else if (Obj1.age < obj2.age)
{
4 5
return nsorderedascending;
}else
{
return nsorderedsame;
}
*/
}];
NSLog (@ "After sorting:%@", NEWARR);
Nsarray and NSString conversions
Nsarray *arr = @[@ "LJ", @ "LM", @ "JJJ"];
How to stitch each element in an array into a string
Method One
1. Define a variable string to save the result after stitching
nsmutablestring *STRM = [nsmutablestring string];
2. Iterate through the array, take each element in the array, add the element to the variable string
For (NSString *str in arr) {
[StrM APPENDSTRING:STR];
//3. Add another one after each add-
[StrM appendstring:@ "-"];
}
[StrM Deletecharactersinrange:nsmakerange (Strm.length-1, 1)];
NSLog (@ "StrM =%@", StrM);
Method Two:
Link an array element to a string, using a * * as a concatenation character to stitch an array element into a string
NSString *str = [Arr componentsjoinedbystring:@ "* *"];
NSLog (@ "str =%@", str);
How to generate an array from a string
String Segmentation method
NSString *str = @ "LNJ**LMJ**JJJ";
Nsarray *arr = [str componentsseparatedbystring:@ "* *"];
NSLog (@ "arr =%@", arr);
Nsarray file read/write
How to write an array to a file
Nsarray *arr = @[@ "ln", @ "LJ", @ "JJ"];
BOOL flag = [arr writetofile:@ "/users/xiaomage/desktop/abc.plist" atomically:yes];
NSLog (@ "flag =%i", flag);
What are the considerations for writing an array to a file
Note 1: If an array is written to a file, it is essentially written to an XML file in iOS development generally we will save the XML file extension as Plist
Note that 2:writetofile can only write to the elements that are saved in the array are objects created by classes in the foundation framework and cannot be written if the custom object is saved
How to read data from a file into Nsarray
Reads an array from a file, which is often used in dictionary-to-model
Nsarray *newarray = [Nsarray arraywithcontentsoffile:@ "/users/xiaomage/desktop/abc.plist"];
NSLog (@ "%@", NewArray);
Nsmutablearray
"Variable array additions and deletions"
What is a mutable array? What's the difference with Nsarray?
Nsmutablearray is a subclass of Nsarray
Nsarray is immutable, once initialized, the contents of it will always be fixed, can not delete the elements inside, and can no longer add elements inside
Nsmutablearray is variable, can add \ change \ delete element at any time
How do I create an empty array? To create a mutable array what's the point of note
1. Creating an empty array
Nsmutablearray *ARRM = [Nsmutablearray array];
Nsmutablearray *ARRM = [[Nsmutablearray alloc] init];
2. Note the point:
You cannot create a mutable array by @[], because @[] creates an immutable array
If you use an immutable group as a mutable array, a run-time error is raised
For example:
Nsmutablearray *arrm = @[@ "LJ" @ "LM"];
[Arrm addobject:@ "JJJ"];
How to add content to a mutable array
[Arrm addobject:@ "LJ"];
Method Two:
The elements in the specified array are removed and placed in ARRM \
Not adding an entire array as an element to the ARRM
[Arrm addobjectsfromarray:@[@ "LJ", @ "JJ"];
Note: The following is the addition of an entire array as an element
[Arrm addobject:@[@ "LJ", @ "JJJ"];
NSLog (@ "%@", ARRM);
How to insert content into a mutable array
[Arrm insertobject:@ "Xcq" atindex:1];
Nsrange range = Nsmakerange (2, 2);
Nsindexset *set = [Nsindexset indexsetwithindexesinrange:range];
Inserts a set of data, specifies where the array needs to be inserted, and inserts how many
[Arrm insertobjects:@[@ "A", @ "B"] atindexes:set];
How to delete content in a mutable array
To delete the specified element:
[Arrm removeobjectatindex:0];
Delete the last element in an array
[Arrm Removelastobject];
Delete an element of the index position
[Arrm removeobject:@ "A"];
How to replace content in a mutable array
[Arrm replaceobjectatindex:1 withobject:@ "M"];
Shorthand:
Arrm[0] = @ "ZS";
NSLog (@ "%@", ARRM);
How to get the contents of a mutable array
NSLog (@ "%@", [Arrm objectatindex:0]);
36-oc Nsarray