1. What is block?
① block syntax, essentially an anonymous function (a function without a name);
②block is a data type in OC and is widely used in iOS development.
③^ is the unique mark of block;
The implementation code of the ④block is included in the {};
⑤ in most cases, the way inline functions are defined and used;
⑥block is somewhat similar to the C-language function pointer, but it is more flexible to use;
Example: In the main.m file:
Console output:
2. What is the block variable stored?
Simply speaking, the block variable is stored: the implementation of the function;
3. How do I define a block variable? How to assign a value to a block variable. If you now have an anonymous function int (NSString * a,int b), its function is to convert the number string A, to an integer value, and then add to B, return and value.
Example: In the main.m file:
Console output:
4. For the anonymous function of the above question, how to execute this anonymous function through the block variable implementation.
Example: In the main.m file:
5. Can I modify the value of a local variable in a block? Can I modify the value of a global variable?
① cannot, need to add _ _block when defining local variables;
② can modify global variables directly in block.
6. Create 3 student objects, which are stored in the array, and how the array is implemented in descending order of age by the block.
Example: In the main.m file:
Console output:
7, Void (^myblock) () = ^void () {NSLog (@ "Hello lanou!");}
Where: What is the type of the ①block variable? What is the variable name of the ②block? What is the type of the value stored by the ③block variable?
Answer: ①void (^) () ②myblock③^void () {NSLog (@ "Hello lanou!");};
Email:[email protected]
OBJECTIVE-C Quick Start-Basic (iv)