1. The traversal? For, enumerator, Forin.
①.for loops the same as C, the loop condition is the number of elements of the set or the number of key-value pairs, and the unordered set is first converted to an array in accordance with the subscript traversal
②. Enumerator Nsenumerator
Objectenumerator (reverseobjectenumerator) Create enumerator (reverse enumerator)
Nextobject with the while loop, iterating through the collection
③. For ... In: Fast enumeration is a more convenient way to encapsulate the instant and element in the Nsenumerator based on the format as follows:
For (< #type *object#> in < #collection #>) {
< #statements #>
}
Type *object:type * represents the object types obtained from the collection
The object is just a name, and you just need to satisfy the variable name naming law
Collection represents the collection of traversal
actions to be performed in statements traversal
For in traversal considerations:
The for in traversal can use nesting, which is commonly used in dictionaries, traversing the key value, and, if it is a collection, traversing the set element in value.
The for-in traversal is equivalent to an enumerator, so you cannot modify the value while traversing, but you can only extract it with something else and then assign it back.
2, the difference between the three?
①. To know the number of objects in a container
②. Enumerators, usually connected with the while loop, Nextobject continue to hold the enumeration as long as it gets to the object, until it is empty.
③. Forin internally encapsulates an enumerator that Forin and enumerators do not allow to change the capacity of the collection while traversing.
3. using nssortdescriptor to sort an array three steps away
1. Create an array to sort
2, create a sort condition, that is, a nssortdescriptor object, the initialization needs to specify what attribute values in the array to sort, ascending or descending
/// The first parameter is an array of what attributes the object is to be sorted by (such as itself, name, age)
///The second parameter specifies whether the sort order is Ascending or descending
3, the array is sorted according to the sortingcriteria, to get a sorted array (if it is a mutable array, does not generate a new array, or itself)
4. How to create a Nssortdescriptor object (Initwithkey method)
①. Sortedarrayusingdescriptor:@[nssortdescriptor objects, that is, sort by]
②. sortedarrayusingdescriptors can be followed by multiple Nssortdescriptor objects, which have higher precedence between objects.
③. Sortedarrayusingselector:sel (@selector (? Dharma name)) must first have a sorting method in order to use the
Note: The parameters of the//SEL type can only be given by @selector ( method name ) .
If the array uses this method to sort, this method must be a return value of Nscomparisionresult.
// array ordering the method parameter to be passed in is the return value of the method, depending on the return value of the method, whether it is arranged in ascending or descending order.
5, guessing the implementation of the default sorting method (compare:)
/* Bubble Sort Interior
if ( expression > 0) {
Exchange ;
} */
The return value of the// method is 1, indicating that the previous isgreater than the following, descending, then the value of the expression is 1, after the exchange of the front is less than the back, so it is ascending,compare: method is the default ascending order.
The return value of the// method is 2, indicating that the previous isless than the following, ascending, then the value of the expression is -1, is not exchanged, or ascending.
OC Sixth, iterating through the collection, sorting the array