Traversal of a collection
1.for Loop traversal
2. Enumerator traversal
1Nsarray *enumarray = @[@"CM",@"PL",@"Lina",@"NEC"];2 //creates an enumerator that is attached to an array3Nsenumerator *rator =[Enumarray Objectenumerator];4 //Create an Object5 ID Object=Nil;6 while(Object=[Rator Nextobject])7 {8NSLog (@"%@",Object);9}
You can also reverse enumerate
1 // Reverse Enumeration 2 Nsenumerator *reverserator = [Enumarray reverseobjectenumerator]; 3 while (object = [Reverserator nextobject]) 4 {5 NSLog (@ "%@"object); 6 }
Iterating through the dictionary with the enumerator
1 Rator = [Dict objectenumerator]; 2 while (object = [Rator nextobject]) 3 {4 NSLog (@ "%@"object); 5 }
3.forin Cycle
Forin is actually a package based on the enumerator traversal, and the structure is:
for ( type *object in collection)
{
Statements
}
Where collection is the container that needs to traverse, object refers to the element that collection every time, if the object in collection is the same type, then the type of object can be specified, statements is the loop body. Like what:
1 for inch Array) 2 {3 NSLog ("%@", str); 4 }
Sorting of collections
1.NSSortDescriptor
1 /* * 2 * using Nssortdescriptor for array sorting there are three steps 3* 1. Create an array to sort 4* 2. To create a sort condition, the initialization needs to specify what attribute values of the objects in the array are sorted, ascending or descending by 5* 3. The array is sorted according to the sort criteria, and a sorted array is obtained ( if it is a mutable array, Does not generate a new array, or itself )6* /
1Nsarray *array = @[@"PA",@"AM",@"Luna",@"DR",@"MED"];2Nssortdescriptor *des = [[Nssortdescriptor alloc] Initwithkey:@" Self"Ascending:yes];3NSLog (@"%@", [array sortedarrayusingdescriptors:@[des]]);
Initialize a Nssortdescriptor object first, Initialize method Initwithkey: There are two parameters. The first representation is based on what sort, where the array itself is used, that is, the elements within the array is sorted, if each element in the array is an object, and this object has a property, then you can use the property name as an argument, you could sort by the property name ; The second parameter indicates ascending or descending.
2.sortedArrayUsingSelector: @selector ()
1 nsarray *newarray = [array sortedarrayusingselector: @selector (compare:)]; 2 NSLog (@ "%@", NewArray);
In the @selector, fill in the method name, similar to the C language function pointer, and the method needs to be the following structure
-(Nscomparisionresult) method name: Parameter
This method is to specify what rules to sort by. For example, here is the system method compare:. The return value of the Compare method is the Nscomparisionresult type, which by default is in ascending order, and if you want to sort in descending order, you can define a method:
1-(Nscomparisonresult) comparedescending: (NSString *) Str2 {3Nscomparisonresult result =[self compare:str];4 if(Result <0)5 {6 return 1;7 }8 Else if(Result >0)9 {Ten return-1; One } A Else - { - return 0; the } -}
And the method name comparedescending: As a @selector parameter can be done in descending order.
"Objective-c Study record" 24th day