ObjectC ---- Block usage and array sorting using blocks

Source: Internet
Author: User

ObjectC ---- Block usage and array sorting using blocks

// Created By Guo Zai April 02, 2015 17:45:01

 

Function in vitro:

Typedef int (^ Block) (int, int); // rename

Typedef void (^ Block1) (int );

Int n = 0;

========================================================== ========================================================== ======================================

// Define the function pointer

Int (* p) (int, int) = sum;

// Call a function through a function pointer

Int re = p (10, 3 );

Printf ("% d \ n", re );

// Int n = 4;

 

Block block = ^ (int x, int y ){

// NSLog (@ "% d", n );

Return x + y;

};

NSLog (@ "% d", block (1, 1 ));

 

// Define the block

Int (^ block1) (int, int) = ^ (int x, int y ){

Return x + y;

}; // Do not forget the last semicolon

 

// Call the block

Int result = block1 (10, 34 );

Printf ("% d \ n", result );

 

// Two hands-on training examples

Void (^ block2) (int) = ^ (int num ){

Printf ("% d \ n", num );

};

Int (^ block3) (int) = ^ (int year ){

Return year;

};

Block2 (4 );

Printf ("% d \ n", block3 (5 ));

 

Int (^ block4) (NSString *) = ^ (NSString * str ){

Return [str intValue]; // The intValue method of the object to convert a string to a value.

};

NSLog (@ "% d", block4 (@ "333 "));

// Printf ("% d \ n", block4 (@" 333 "));

 

 

/*

Block is an anonymous function: because there are two methods to call a function, one is to call the function name, and the other is to call the function pointer;

Before calling a function through a function pointer, You need to assign values to the function pointer and use the function name. Therefore, for anonymous functions, neither of the two methods can call the function, so block is used;

Block and function pointer are very similar, but they are essentially different, but there is little difference in use. We can use block as a function pointer to simplify the use of blcok;

*/

_ Block int x = 10;

// Int y;

Block1 block5 = ^ (int num ){

 

X ++;

// Y ++; // local variable

N ++; // global variable

NSLog (@ "% d", x );

NSLog (@ "% d", num );

};

 

Block5 (5 );

 

 

/*

1. You can access local variables outside the block in the block and cannot modify local variables outside the block. If you need to modify the local variables, add _ block before the local variables;

2. You can modify the global variable value in the block;

3. the variables declared by _ block are stored in the global (static) area of the memory)

*/

 

 

// ================================================ ======================================

 

Car * car1 = [Car carWithType: @ "aa" andColor: @ "bb" andPrice: 134 andMaxSpeed: 133 andCostOil: 17];

 

Car * car2 = [Car carWithType: @ "cc" andColor: @ "dd" andPrice: 234 andMaxSpeed: 233 andCostOil: 27];

 

Car * car3 = [Car carWithType: @ "ee" andColor: @ "ff" andPrice: 334 andMaxSpeed: 333 andCostOil: 37];

 

Car * car4 = [Car carWithType: @ "gg" andColor: @ "hh" andPrice: 434 andMaxSpeed: 433 andCostOil: 47];

 

NSMutableArray * cars = [NSMutableArray arrayWithObjects: car1, car2, car3, car4, nil];

 

// Sort the price by block from high to low

// If sortedArrayUsingComparator is used, you need to redefine an array for storage (because there is a returned value)

[Cars sortUsingComparator: ^ NSComparisonResult (Car * obj1, Car * obj2 ){

If ([obj1 price] <[obj2 price]) {

Return NSOrderedDescending;

}

Else if ([obj1 price] = [obj2 price])

Return NSOrderedSame;

Else

Return NSOrderedAscending;

}];

 

For (Car * car in cars ){

NSLog (@ "% @", car );

}

 

// Sort maxSpeed in ascending order using sortedUsingComparator

NSArray * arr = [cars sortedArrayUsingComparator: ^ NSComparisonResult (Car * obj1, Car * obj2 ){

If ([obj1 maxSpeed] <[obj2 maxSpeed]) {

Return NSOrderedDescending;

}

Else if ([obj1 maxSpeed] = [obj2 maxSpeed])

Return NSOrderedSame;

Else

Return NSOrderedAscending;

}];

 

For (Car * car in arr ){

NSLog (@ "% @", car );

}

 

 

// Customize the block

NSComparisonResult (^ block6) (Car *, Car *) = ^ (Car * obj1, Car * obj2 ){

If ([obj1 maxSpeed] <[obj2 maxSpeed]) {

Return NSOrderedDescending;

}

Else if ([obj1 maxSpeed] = [obj2 maxSpeed])

Return NSOrderedSame;

Else

Return NSOrderedAscending;

 

};

// Call the sort method and pass the parameter in block6.

[Cars sortUsingComparator: block6];

 

For (Car * car in cars ){

NSLog (@ "% @", car );

}

 

// ================================================ ======================================

// Literal

// Array literal

NSString * str = @ "Literals ";

// Array literal

NSArray * arr1 = @ [@ "1", @ "2", @ "3"];

For (NSString * s in arr1 ){

NSLog (@ "% @", s );

}

// Dictionary literal

NSDictionary * dic =@ {@ "name": @ "zhangsan", @ "age": @ "6 "};

NSLog (@ "% @, % @", dic [@ "name"], dic [@ "age"]);

 

 

NSNumber * num = @ 123; // converts the number 123 to an NSNumber object

NSLog (@ "% @", num );

// ================================================ ======================================



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.