1. How do I define a block?
The left side of the equals sign is the declaration to the right.
Declaration part: First write the return value type then (Block) then (parameter name); Be sure to enclose the parentheses.
Implementation section: ^ toeic character return value type
2.how block is applied : array sorting
Issues to be aware of:
1 "Object" is used when comparing.
Like what:
Student *STU1 = [[Student alloc]initwithname:@ "Wangbadan" withsex:@ "nan" withage:13 withscore:77 Withnumber:3];
Nsarray *array = [Nsarray arraywithobjects:stu1,stu2,stu3, Nil];
Note that this is used when stu1 (object) instead of @ "STU1"
2 when a non-changing comparison is made, there is a return value type of
Array = [array sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2)
If written in this way is wrong:
[Array sortedarrayusingcomparator:^nscomparisonresult (ID obj1, id obj2)
Because the return value type of sortedarrayusingcomparator is an array, you must use an array to receive it. no sorting is done from the results of the output
3 Think (for in) loop output
4 What to do when you do the project:
Block is implemented using the C-language structure (it can be researched when doing the project)
Nsinteger __block count = 0; (add two bars in front of block)
Nsinteger count = 0;
Nsinteger (^stringblock) (NSString *) = ^nsinteger (NSString *string) {
Nsinteger number = [string IntegerValue];
Count++;//block the internal copy process, a local variable is copied over as a constant to use (this notation is changed block, if not added Nsinteger __block count = 0, to achieve, if not add is wrong. )
CountNumber = countNumber-5;
Global variables can be used inside the block or modified
Number =number +count+countnumber;
return number;
};
Nsinteger result= Stringblock (@ "3");
NSLog (@ "%lu", result);
If you want to change the local variables of the block, write the __block on it.
OC Learning Day Sixth (some errors encountered while knocking the code)