iOS -

1. Category

  Category is to add methods to the original class, can not add attributes

  @Property in the category will only generate getter.setter declarations, and will not generate method implementations and member variables.

  Classification can access the properties declared in the original class.h

  If a method in a category has the same name as the original method, then calling the method with the original class is actually calling the method of the category.

  If multiple categories have the same method as the original class, then the compiler decides which one to call when calling. The method in the last category involved in the compilation is called.

2. Extension

  You can extend private member variables and methods for the class

  Written in .m

  @interface class name ()

  @end

3. Block

  Block is a special data type of iOS, used to save a certain piece of code, and then call it out when needed

  

Return value type (^ blockName) (formal parameter list); 
blockName = ^ (formal parameter list) {code}; // save code 
blockName = ^ {}
block (); // call
typedef int (^sumBlock)(int,int);
sumBlock = ^(int num1, int num2){ return num1+ num2;};
sumBlock(2,3);

 Block can access external variables, and can also define the same variable names as external variables, but by default, external variables cannot be modified (a pass value)

block accesses the external variables, the block will copy the external variables to the heap memory

int a = 10 ; 
 void (^ block) () = ^ { 
    NSLog ( " % i " , a);   // copy a copy of a = 10 to heap memory 
}; 
a = 20 ; 
block (); // this When printing 10

If the block wants to modify the value of the external variable, you need to add __block in front of the external variable. At this time, if the block changes the value of the external variable, it will affect the value of the external variable (address transfer value & a)

By default, block 

Guess you like

Origin www.cnblogs.com/yintingting/p/12675983.html
ios