[Oc learning notes] array traversal and sorting, oc learning notes

Source: Internet
Author: User
Tags array example

[Oc learning notes] array traversal and sorting, oc learning notes

1 // array traversal (variable array example) 2 // for traversal 3 NSMutableArray * aa = [NSMutableArray array]; 4 [aa addObjectsFromArray: @ [@ "ss ", @ "ddd", @ "sad"]; 5 for (int I = 0; I <aa. count; I ++) {6 NSLog (@ "% @", [aa objectAtIndex: I]); 7} 8 // fast traversal 9 for (id obj in aa) {10 NSLog (@ "% @", obj); 11} 12 // code block traversal 13 // obj is the object idx is the subscript stop is the stop sign 14 [aa enumerateObjectsUsingBlock: ^ (id obj, NSUInteger idx, BOOL * stop) {15 NSLog (@ "% @", obj); 16 NSLog (@ "% @", [aa objectAtIndex: idx]); 17}]; 18 // iterator traversal 19 // store array elements into iterator 20 NSEnumerator * en = [aa objectEnumerator]; 21 id obj = nil; // create an object to accept the returned object 22 while (obj = [en nextObject]) {23 NSLog (@ "% @", obj ); 24} 25 // The Reverse iterator traverses 26 NSEnumerator * en1 = [aa reverseObjectEnumerator]; 27 id obj1 = nil; 28 while (obj1 = [en1 nextObject]) {29 NSLog (@ "% @", obj1); 30} 31 32 33 // sort (variable array example) 34 // variable array sorting method no return value immutable array sorting return value 35 // selector sorting 36 // when the comparison element is a character, if there is another comparison, please write another comparison method return value (NSComparisonResult) 37 [aa sortUsingSelector: @ selector (compare :)]; 38 // code block sorting 39 // write judgment method based on actual conditions 40 [aa sortUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {41 return [obj1 compare: obj2]; 42}]; 43 // The comparison descriptor sorts 44 // It is easier to sort multiple conditions 45 Person * p1 = [[Person alloc] init]; 46 p1.name = @ "ss"; 47 p1.age = @ "16"; 48 49 Person * p2 = [[Person alloc] init]; 50 p2.name = @ "dd "; 51 p2.age = @ "14"; 52 53 Person * p3 = [[Person alloc] init]; 54 p3.name = @ "hh"; 55 p3.age = @ "22 "; 56 57 Person * p4 = [[Person alloc] init]; 58 p4.name = @ "hh"; 59 p4.age = @ "19"; 60 NSMutableArray * sss = [NSMutableArray arrayWithObjects: p1, p2, p3, p4, nil]; 61 NSSortDescriptor * sort1 = [NSSortDescriptor sortDescriptorWithKey: @ "name" ascending: YES]; 62 NSSortDescriptor * sort2 = [NSSortDescriptor: @ "age" ascending: YES]; 63 NSArray * s = @ [sort1, sort2]; 64 [sss sortUsingDescriptors: s]; 65 NSLog (@ "% @", sss );

Person. h

1 #import <Foundation/Foundation.h>2 3 @interface Person : NSObject4 @property (nonatomic,strong)NSString *name;5 @property (nonatomic,strong)NSString *age;6 @end

Person. m

1 #import "Person.h"2 3 @implementation Person4 - (NSString *)description5 {6     return [NSString stringWithFormat:@"%@ %@", _name,_age];7 }8 @end

Running result analysis:

2015-06-01 17:37:55.984 Array[2030:140148] ss2015-06-01 17:37:55.985 Array[2030:140148] ddd2015-06-01 17:37:55.985 Array[2030:140148] sad2015-06-01 17:37:55.986 Array[2030:140148] ss2015-06-01 17:37:55.986 Array[2030:140148] ddd2015-06-01 17:37:55.986 Array[2030:140148] sad2015-06-01 17:37:55.986 Array[2030:140148] ss2015-06-01 17:37:55.986 Array[2030:140148] ss2015-06-01 17:37:55.986 Array[2030:140148] ddd2015-06-01 17:37:55.987 Array[2030:140148] ddd2015-06-01 17:37:55.987 Array[2030:140148] sad2015-06-01 17:37:55.987 Array[2030:140148] sad2015-06-01 17:37:55.992 Array[2030:140148] ss2015-06-01 17:37:55.992 Array[2030:140148] ddd2015-06-01 17:37:55.992 Array[2030:140148] sad2015-06-01 17:37:55.992 Array[2030:140148] sad2015-06-01 17:37:55.992 Array[2030:140148] ddd2015-06-01 17:37:55.993 Array[2030:140148] ss2015-06-01 17:37:55.993 Array[2030:140148] (    "dd 14",    "hh 19",    "hh 22",    "ss 16")

 

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.