Some basic methods of arrays
intCount = [array count];//number//determine if an element is includedif([Array Containsobject:@"a"]) {NSLog (@"contains the string a"); } nsstring*last =[Array Lastobject]; the last element NSString*STR = [Array Objectatindex:1]; Gets the elements in the array according to the indexintindex = [Array indexofobject:@"C"]; Gets the index of the specified element//let all objects inside the array call the test method, 123 is the parameterNsarray *array =[Nsarray arraywithobjects:stu1, STU2, STU3, nil Nil]; [Array makeobjectsperformselector: @selector (test2:) Withobject:@"123"]; Nsarray*array = [Nsarray arraywithobjects:@"1",@"2",@"3",@"4", nil Nil]; //1-2-3-4//use separators-to stitch all array elementsNSString *str = [Array componentsjoinedbystring:@"-"]; //writes an array to a file (an XML file is generated)NSString *path =@"/users/apple/desktop/array.xml"; [Array Writetofile:path atomically:yes]; Path=@"/users/apple/desktop/array.txt"; //reading array contents from a file (file has strict formatting requirements)Nsarray *array2 = [Nsarray Arraywithcontentsoffile:path];
Iterating through an array
#pragmaMark iterates through the array 1voidArrayFor1 () {Nsarray*array = [Nsarray arraywithobjects:stu1,@"1",@"2",@"3", nil Nil]; intCount =Array.count; for(inti =0; i<count; i++) { IDobj =[Array objectatindex:i]; } } #pragmaMark iterates through the array 2 fast traversalvoidArrayFor2 () {Student*STU1 =[Student Student]; Nsarray*array = [Nsarray arraywithobjects:stu1,@"1",@"2",@"3", nil Nil]; inti =0; for(IDObjinchArray) {NSLog (@"%i-%@", I, obj); I++; } } #pragmaMark iterates through the array 3voidArrayFor3 () {Student*STU1 =[Student Student]; Nsarray*array = [Nsarray arraywithobjects:stu1,@"1",@"2",@"3", nil Nil]; [Array Enumerateobjectsusingblock:^(IDobj, Nsuinteger idx, Boolbool *stop) {NSLog (@"%i-%@", idx, obj); //if the index is 1, stop traversing if(idx = =1) { //using pointers to modify the value of a bool variable outside*stop =YES; } }]; } #pragmaMark iterates through the array 4voidArrayFor4 () {Student*STU1 =[Student Student]; Nsarray*array = [Nsarray arraywithobjects:stu1,@"1",@"2",@"3", nil Nil]; //get an iterator to an array//nsenumerator *enumerator = [array objectenumerator]; //Reverse-order iterator (traversing elements from the tail)Nsenumerator *enumerator =[Array reverseobjectenumerator]; //allobjects is to remove objects that have not been traversedNsarray *array2 =[Enumerator allobjects]; NSLog (@"array2:%@", array2); //gets the next element to traverse IDobj =Nil; while(obj =[Enumerator Nextobject]) {NSLog (@"obj=%@", obj); } }
Array sorting
#pragmaMark Array Sort 1voidArraySort1 () {Nsarray*array = [Nsarray arraywithobjects:@"2",@"3",@"1",@"4", nil Nil]; //returns an ordered array that does not change the order of the elements in the original array//The comparison method for the specified element: compare:Nsarray *array2 =[Array sortedarrayusingselector: @selector (compare:)]; NSLog (@"array2:%@", array2); } #pragmaMark Array Sort 2voidArraySort2 () {Student*STU1 = [Student studentwithfirstname:@"Mingjie"LastName@"Li"]; Student*STU2 = [Student studentwithfirstname:@"Longhu"LastName@"Huang"]; Student*STU3 = [Student studentwithfirstname:@"Lianjie"LastName@"Li"]; Student*stu4 = [Student studentwithfirstname:@"Jian"LastName@"Xiao"]; Nsarray*array =[Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil]; //Specify a comparison method for sortingNsarray *array2 =[Array sortedarrayusingselector: @selector (comparestudent:)]; NSLog (@"array2:%@", array2); } -(Nscomparisonresult) Comparestudent: (Student *) Stu {//Sort by last name firstNscomparisonresult result =[Self.lastname Compare:stu.lastname]; //If you have the same last name, compare the names . if(Result = =nsorderedsame) {Result=[Self.firstname Compare:stu.firstname]; } returnresult; } #pragmaMark Array Sort 3voidArraySort3 () {Student*STU1 = [Student studentwithfirstname:@"Mingjie"LastName@"Li"]; Student*STU2 = [Student studentwithfirstname:@"Longhu"LastName@"Huang"]; Student*STU3 = [Student studentwithfirstname:@"Lianjie"LastName@"Li"]; Student*stu4 = [Student studentwithfirstname:@"Jian"LastName@"Xiao"]; Nsarray*array =[Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil]; //Sort by blockNsarray *array2 =[Array sortedarrayusingcomparator:^nscomparisonresult (Student *obj1, Student *obj2) { //Sort by last name firstNscomparisonresult result =[Obj1.lastname Compare:obj2.lastname]; //If you have the same last name, compare the names . if(Result = =nsorderedsame) {Result=[Obj1.firstname Compare:obj2.firstname]; } returnresult; }]; NSLog (@"array2:%@", array2); } #pragmaMark Array Sort 4-advanced sortvoidArraySort4 () {Student*STU1 = [Student studentwithfirstname:@"Mingjie"LastName@"Li"BookName:@"Book1"]; Student*STU2 = [Student studentwithfirstname:@"Longhu"LastName@"Huang"BookName:@"Book2"]; Student*STU3 = [Student studentwithfirstname:@"Lianjie"LastName@"Li"BookName:@"Book2"]; Student*stu4 = [Student studentwithfirstname:@"Jian"LastName@"Xiao"BookName:@"Book1"]; Nsarray*array =[Nsarray arraywithobjects:stu1,stu2,stu3, Stu4, nil Nil]; //1. Sort by Title first//The key here is the name of @property.Nssortdescriptor *booknamedesc = [Nssortdescriptor sortdescriptorwithkey:@"Book.name"Ascending:yes]; //2. Sort by last nameNssortdescriptor *lastnamedesc = [Nssortdescriptor sortdescriptorwithkey:@"LastName"Ascending:yes]; //3. Sort by name againNssortdescriptor *firstnamedesc = [Nssortdescriptor sortdescriptorwithkey:@"FirstName"Ascending:yes]; //Add a sort descriptor sequentiallyNsarray *descs =[Nsarray Arraywithobjects:booknamedesc, Lastnamedesc, Firstnamedesc, nil Nil]; Nsarray*array2 =[Array Sortedarrayusingdescriptors:descs]; NSLog (@"array2:%@", array2); }
Some basic operations of iOS development arrays, traversing, sorting