Block Grammar Exercises

Source: Internet
Author: User

A function is a code snippet with a specific function

A strict function, including function declaration, function definition, function call

Block syntax, is within the function, define a block, block blocks execution efficiency is higher

The function pointer points to the first address, so the pointer can call the function

Renaming a function pointer type

typedef function return value type (* new function name) ()

Block blocks Syntax

return value type (^ block syntax name) (parameter type 1, parameter name 1 argument type 2, parameter name 2, ...) ()

The argument type is separated by a space from the parameter name, and two parameters are separated by commas = ^ (parameter type 1 parameter 1, parameter type 2 parameter 2, ...)

Int (^ block syntax name) (parameter type, parameter name) = ^ (argument type, parameter name) {//The argument preceding the equals sign can be omitted

return ...;

// };

Two-digit maximum value

Int (^max) (int number1, int number2) = ^ (int number1, int number2) {

int max = number1 > number2? Number1:number2;

return Max;

};

NSLog (@ "%d", Max (100, 120));

Int (^maxnumber) (int number1, int number2) = ^ (int number1, int number2) {

int maxnumber = number1 > number2? Number1:number2;

return maxnumber;

//    };

NSLog (@ "%d", MaxNumber (100, 120));

Implementation converts a string to an integer type

Int (^change) (NSString *) = ^ (NSString *str2) {

int str1 = [str2 intvalue];

return str1;

};

NSLog (@ "%d", change (@ "123"));

The ascll of the corresponding characters;

Nsinteger (^ACLL) (char) = ^ (char c) {

Nsinteger a = c;

return A;

};

NSLog (@ "%ld", ACLL (' C '));

If you print a character with%d, you will get the ASCLL value of this character.

Int (^middle) (int, int, int) = ^ (int num1, int num2, int num3) {

int max = (Num1 > num2? num1:num2) > num3? (Num1 > Num2 num1:num2): num3;

int max = NUM1 > num2? (Num1 > Num3 num1:num3): (Num2 > num3? num2:num3);

referencing macro Definitions

int max = max (NUM1, Max (num2, num3));

int min = Num1 < num2? (Num1 < num3 num1:num3): (Num2 < num3? num2:num3);

Return num3 + num2 + num1-max-min;

};

NSLog (@ "%d", Middle (4, 3, 6));

The value of the local variable cannot be modified inside the block, and if you want to modify the value of the local variable, change the variable to a global variable, or modify the variable with __block

Sorting of arrays

Compare: The method provides a comparison of two adjacent elements, which can only be arranged in ascending order,

Provides the criteria for the comparison rule nsorderedascending of adjacent elements, that is, the order of the last arrangement

The notation 1//a rule by means of a block grammar.

Nsarray *array = [Nsarray arraywithobjects:@ "@", @ "4" @ "@" @ "@" @ "@" @ "$", nil];

Nscomparisonresult (^rule) (NSString *, nsstring *) = ^ (NSString *str1, NSString *str2) {

if ([str1 intvalue] < [str2 Intvalue]) {

Return nsorderedascending;//Rise

}else if ([str1 intvalue] = = [str2 intvalue]) {

return nsorderedsame;

}else{

return nsordereddescending;

}

};

1.

Nsarray *sortarray = [Array sortedarrayusingselector: @selector (compare:)];

2.

Nsarray *sortarray = [array sortedarrayusingcomparator:rule];

NSLog (@ "%@", Sortarray);

Student *STU1 = [[Student alloc] initwithname:@ "Chengqiang" num:2];

Student *STU2 = [[Student alloc] initwithname:@ "Bengbeng" num:3];

Student *STU3 = [[Student alloc] initwithname:@ "Dengdeng" num:4];

Nsarray *marray = [[Nsarray alloc]initwithobjects:stu1, STU2, STU3, nil];

Nscomparisonresult (^rule1) (Student *, Student *) = ^ (Student *stu1, Student *stu2) {//Custom block syntax

if ([stu1 num] > [stu2 num]) {

return nsorderedascending;

}else if ([stu1 num] = = [Stu2 num]) {

return nsorderedsame;

}else{

return nsordereddescending;

//        }

return [[stu1 name] COMPARE:[STU2 name]];

};

Nsarray *msortarray = [Marray sortedarrayusingcomparator:rule1];

For (Student *stu in Msortarray) {

NSLog (@ "%@,%ld", [stu name], [Stu Num]);

}

Nsarray *karray = [Msortarray sortedarrayusingcomparator:^nscomparisonresult (Student *obj1, Student *obj2) {

if ([stu1 num] > [stu2 num]) {

return nsorderedascending;

}else if ([stu1 num] = = [Stu2 num]) {

return nsorderedsame;

}else{

return nsordereddescending;

//        }

return [[stu1 name] COMPARE:[STU2 name]];

}];

For (Student *stu in Karray) {

NSLog (@ "%@,%ld", [stu name], [Stu Num]);

}

Grammar sugar

NSNumber *num = [NSNumber numberwithint:100];//converts a value to an object

Quickly create a dictionary, arrays and dictionaries created with syntactic sugars are immutable

Nsdictionary *dic = @{@ "name": @ "Xiao Ming", @ "gender": @ "Male" @ "age": @ "18"};

Take the value in the dictionary

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

Block Grammar Exercises

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.