"Objective-c Study record" 24th day

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.