Objective-C ---- Block, array sorting, objectivec Array
Directly run the Code:
// Write into a block whose return value is NSString (only contains one parameter // number) to convert the string to an integer. // Int (NSString * string) {// return [string intValue]; // assign ^ int (NSString * string) to int (^ myBlock) (NSString *) // ^ int (NSString * string) is an anonymous function whose parameter is of the NSString * type and return value is of the int type. // It can be defined in main () data Types outside the function // block represent the formats of anonymous functions (return value type, shape parameter type) // The definition of block variables is similar to that of function pointer variables, the only difference from the function pointer variable is that the variable name is modified by the escape character '^. // First, use the ^ modifier. The remaining part is consistent with the c Function Definition. The biggest difference is that there is no function name (the return value type can also be omitted) // block variables have the basic features of variable definition. The anonymous function on the right of the value assignment number can be assigned as a whole, similar to int a = 5; // The value assigned by the block variable is an anonymous function and has both the features of the function. It is unique and can be defined within a function implementation. (in C language, the function cannot be defined in nesting mode, block is a special case) int (^ myBlock) (NSString *) = ^ int (NSString * string) {return [string intValue] ;}; NSLog (@ "% d ", myBlock (@ "123") + 1); // int (^ sumBlock) (int, int) = ^ (int number1, int number2) This is also OK int (^ sumBlock) (int, int) = ^ int (int number1, int number2) {return number1 + number2 ;}; NSLog (@ "% d", sumBlock (1, 2 ), sumBlock (3, 4); // similar to the type definition of a function pointer, the format is the same as that of a function pointer, the Type Definition simplifies block usage to some extent // typedef int (^ BlockType) (int, int); BlockType sum = ^ int (int n, int m) {return m + n ;}; NSLog (@ "% d", sum (2, 3 )); // _ block indicates that the defined variables can be used in the block function; // _ block type identifier allows local variables to be normally accessed within the block defined later. // (Note: Set _ block int count to 0; it cannot be defined after the _ block Function body) _ block int count = 8; void (^ testBlock) () = ^ () {// int count = 9; // _ block int count = 6; // XXXX cuo wu for (int I = 0; I <10; I ++) {count ++; NSLog (@ "% d", count) ;}; // _ block int count = 8; // NSLog (@ "% d", count ); // block calls testBlock (); NSComparisonResult (^ comareResult) (NSString *, NSString *) = ^ (NSString * str1, NSString * str2) {return [str1 compare: str2] ;}; NSLog (@ "% ld", comareResult (@ "456", @ "123 ")); /** array sorting */NSArray * array = @ [@ 1, @ 3, @ 2, @ 5, @ 4]; NSArray * resultArray1 = [array sortedArrayUsingSelector: @ selector (compare :)]; NSLog (@ "% @", resultArray1); _ block int number = 0; NSComparator sortBlock = ^ (id obj1, id obj2) {number ++; return [obj2 compare: obj1] ;}; NSLog (@ "% d", number); // run NSArray * resultArray2 = [array sortedArrayUsingComparator: sortBlock]; NSLog (@ "% d", number); NSLog (@ "% @", resultArray2 ); // You can also write this // NSArray * resultArray3 = [array ready: ^ (id obj1, id obj2) NSArray * resultArray3 = [array sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {return [obj2 compare: obj1] ;}]; NSLog (@ "% @", resultArray3 ); /** Exercise 2. ** sort the previous example array by student ID in ascending order * 2. sort by student ID in descending order * 3. sort by name in ascending order * 4. sort in descending order */Student * stu1 = [Student studentWithName: @ "wang" number: 1001 age: 23]; Student * stu2 = [Student studentWithName: @ "zhen" number: 1003 age: 22]; Student * stu3 = [Student studentWithName: @ "gang" number: 1002 age: 24]; NSArray * strArray = [[NSArray alloc] initWithObjects: stu1, stu2, stu3, nil]; NSLog (@ "% @", [strArray sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {return [[obj1 getName] compare: [obj2 getName] ;}]); NSLog (@ "% @", [strArray sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {return [[NSString stringWithFormat: @ "% d", [obj1 getNumber] compare: [NSString stringWithFormat: @ "% d", [obj2 getNumber];}]); NSLog (@ "% @", [strArray sortedArrayUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {return [[NSString stringWithFormat: @ "% d", [obj1 getAge] compare: [NSString stringWithFormat: @ "% d ", [obj2 getAge];}]); NSMutableArray * stus = [NSMutableArray array]; NSArray * names = @ [@ "Duke", @ "Douglas ", @ "Allen", @ "Lily", @ "Lucy", @ "Jack", @ "Rose", @ "Jeams", @ "Tom ", @ "Frank"]; for (int I = 0; I <10; I ++) {Student * stu = [Student studentWithName: names [I] number: arc4random () % 9000 + 1000 age: arc4random () % 100]; [stus addObject: stu];} NSLog (@ "% @", stus); // Note: difference: [strArray objects: ^ NSComparisonResult (id obj1, id obj2) [stus sortUsingComparator: ^ NSComparisonResult (id obj1, id obj2) {// return [[obj1 getName] compare: [obj2 getName]; NSInteger age1 = [obj1 getAge]; NSInteger age2 = [obj2 getAge]; if (age1 <age2) {return NSOrderedDescending;} else if (age1> age2) {return NSOrderedAscending;} return NSOrderedSame;}]; NSLog (@ "% @", stus );
Student. h file:
//// Student. h /// Created by on 15/4/7. // Copyright (c) 2015. all rights reserved. // # import <Foundation/Foundation. h> @ interface Student: NSObject {NSString * _ name; // name int _ number; // Student ID int _ age; // age @ protected NSString * _; @ private NSString * _ B;}-(instancetype) initWithName :( NSString *) name number :( int) number age :( int) age; + (instancetype) studentWithName :( NSString *) name number :( int) number age :( int) age;-(NSString *) getName;-(int) getNumber;-(int) getAge; @ end // Student
Student. m file:
//// Student. m /// Created by on 15/4/7. // Copyright (c) 2015. all rights reserved. // # import "Student. h "@ implementation Student-(instancetype) initWithName :( NSString *) name number :( int) number age :( int) age {if (self = [super init]) {_ name = name; _ number = number; _ age = age;} return self;} + (instancetype) studentWithName :( NSString *) name number :( int) number age :( int) age {return [[Student alloc] initWithName: name number: number age: age];}-(NSString *) getName {return _ name;}-(int) getNumber {return _ number;}-(int) getAge {return _ age;}-(NSString *) description {return [[NSString alloc] initWithFormat: @ "% @ % d", _ name, _ number, _ age] ;}@ end // Student