One: Overview
Nsarray is an immutable group, once the array is established, the array elements cannot be added or deleted, that is, the length of the array cannot be changed, and the values of the elements in the array cannot be modified, only the values that were originally created.
Nsmutablearray is a mutable array, which is a subclass of Nsarray, which can increment, delete, and change elements of the array.
Two: Several ways to create an array
//C-language arraysNSString *num[3]={@" First",@"Second",@"Third"}; // The last element to create an array must be nil Nsarray *student = [Nsarray arraywithobjects:@"AAA",@"BBB",@"CCC",@"DDD", nil];//initializing with an array that has already been createdNsarray *stucopy=[Nsarray arraywitharray:student]; //using c array initializationNsarray *numcopy=[nsarray arraywithobjects:num Count:3];
The arrays created by these methods are all initialized with the source array, and the new array is not in the same block area as the source array in memory .
Three: common methods of arrays
1. Array sorting (performs a sort operation on an array, returning a new one that has no effect on the ordering of the elements in the source array)
1 //Creating an element string2NSString *data=@"15,13,18,9,23";3 4 //array of string turns5Nsarray *temp=[data componentsseparatedbystring:@","];6 7 //Sort Arrays8Nsarray *array=[temp Sortedarrayusingcomparator:^nscomparisonresult (IDObj1,IDobj2) {9 Ten //encapsulates an element as a NSNumber object that can be compared by numeric size OneNSNumber *number1=[NSNumber numberwithint:[obj1 intvalue]; ANSNumber *number2=[NSNumber numberwithint:[obj2 intvalue]; - - //to compare theNscomparisonresult result=[Number1 compare:number2]; - - //if Obj1 and obj2 are sorted in descending order, the returned result is 1, that is, the source array is sorted in ascending order. - returnresult = = Nsordereddescending;//sort in ascending order + //return result = = Nsorderedascending; //sort in descending order -}];
2. Determine if the array contains the given element.
Implementation principle: Using-containsobject: (ID) Object This method, in the method to traverse the entire array, to each array element sent-isequal: (ID) Object This method, if the result of an element is true, then return Yes, Otherwise, no is returned.
-isequal method in judging NSString and NSNumber detailed:
The-isequal method is sure to return true if the two-judged object pointers are equal, so there are several cases where the pointers are unequal and the values are equal.
(1) Judging the situation of nsstring
The only condition that determines whether a string is equal is the hash value of the string. So as long as the value of the string is equal, the result is true regardless of whether the pointer to it is equal.
ps:nsstring *[email protected] "ABC";
NSString *str2=[nsstring stringwithformate:@ "abc"];
STR1 is in the constant area, and the pointer to the STR2 in the heap area, two strings is different, but the value of the string is @ "abc". then [str1 hash] = = [STR2 hash]. The result of [str1 ISEQUAL:STR2] is also true.
(2) Judging the situation of NSNumber
The first is the pointer assignment at the time of creation, and if NSNumber is created in the same way as if the values are equal, the same pointer is returned in memory nsnumber.
Ps:nsnumber *number1=[nsnumber Numberwithintvalue:1];
NSNumber *number2=[nsnumber Numberwithintvalue:1];
Then Number1 and Number2 point to the same area in memory, which is number1 = = number2.
Ps:nsnumber *number1=[nsnumber Numberwithintvalue:1];
NSNumber *number2=[nsnumber numberwithdouble:1.0];
At this point the values of number1 and number2 are not equal, number1! = number2. But [number1 hash] = = [Number2 hash]. The same [number1 Isequal:number2] result is true.
so-isequal, when judging nsstring and nsnumber, directly consider whether the value is equal, regardless of whether the two are pointing to the same object in memory .
(3) Judging the situation of Nsindexpath and the same as in the last two cases, as long as the values of two objects are equal, regardless of whether the same object in memory, the result of isequal and so on is true
(4) In judging the general custom class, if the IsEqual method is not overridden, the system simply determines whether the pointer is equal, that is, whether it is the same object in memory. If the IsEqual method is rewritten, there are some prescribed formats and requirements.
System default Award, etc.:
-(BOOL) IsEqual: (ID) other { return self = = other ; } - (Nsuinteger) hash { return (Nsuinteger) self; }
3. Enumerating through arrays
-objectsenumerator; Get Enum Object
-reverseobjectenumerator; Get a Reverse enumeration object
//Nsenumerator ExampleNsarray *array = [Nsarray arraywithobjects:@" One",@" Both",@"three",@" Four", nil];//Generating EnumerationsNsenumerator *enumerator =[Array objectenumerator];//Get Enumeration Object IDthing; while(thing =[Enumerator Nextobject]) {NSLog (@"I found%@", Thing); }
Fast enumeration for (type newvariable in expression) {//todo}, fast enumeration used to modify the monitor ,
If you attempt to modify the collection during enumeration, an exception is generated .
4. Array elements combined into strings
-(NSString *) componentsjoinedbystring: (NSString *) separator;
iOS Knowledge point Summary one (Nsarray)