Embedded & iOS: callback function (C) and block (OC) callback comparison, embedded training
After learning the OC block, it is awkward to write the C callback function. Compare the difference and record it.
C callback function:
CallBack. h
1) define the number and type of parameters for a callback function.
typedef void (*CallBack)(int data_i32);
2) declare an external function pointer.
extern CallBack myCallBackPTR;
3) declare a doSomeThingCount Function
void DSTCount(void);
CallBack. c
1) define a function pointer
CallBack myCallBackPTR;
2. In the doSomeThingCount function, increase the number of running times and call the function pointer.
void DSTCount(void){ static int numb = 0; numb++; (*myCallBackPTR)(numb);}
Main. c
1) define a customdoSomeThingCount function to print data
void customDSTCount(int data_i32){ printf("%d\n",data_i32);}
2) in the main function, point the callBack. h function pointer to main mdstcount of main and run the DSTCount function of callBack. h.
myCallBackPTR = customDSTCount; while (1){ DSTCount();}
Advantages: 1. You can modify and supplement the callBack file through main or other files without modifying the callBack file.
Purpose: 1. For example, a temperature sensor peripheral. c ,. h has been written. If you don't want others to modify it, set it to read-only. By setting the callback function pointer, you can set the filtering method, or weight filtering, or average value filtering as needed.
Supplement: 1. the callback function in UCOS is like a hook (hooks) function. Is it completely equivalent? I will add it later.
OC Block:
MyButton. h (inherited from UIButton ):
1) define the number and type of parameters of a Block. (Basically similar, c is *, oc is ^)
typedef void(^ButtonBlock)(int data_i32);
2) declare and define a Block variable. (Similar)
@property (nonatomic,strong) ButtonBlock myButtonBlockPTR;
3) declare a doSomeThingCount function. (Similar)
-(void)DSTCount;
MyButton. m (inherited from UIButton ):
1) In the doSomeThingCount function, increase the number of running times and call the Block variable. (Basically similar, C is (* xx) (yy), OC is xx (yy ))
-(void)DSTCount{ static int numb = 0; numb++; _myButtonBlockPTR(numb);}
RootViewControl. m (self. window. rootViewController ):
1) viewDidLoad function, defines a (100,100,100,100), red button instance, write in the Block of the button, print times, button add event, Add button to self. view
MyButton *tempButton = [[MyButton alloc]initWithFrame:CGRectMake(100, 100, 100, 100)]; tempButton.backgroundColor = [UIColor redColor]; tempButton.myButtonBlockPTR = ^(int data_i32){ printf("%d\n",data_i32);};[tempButton addTarget:self action:@selector(mybuttonClick:) forControlEvents:UIControlEventTouchUpInside]; [self.view addSubview:tempButton];
2. click the button to call the DSTCount function in MyButton.
-(void)mybuttonClick:(MyButton*)button{ [button DSTCount];}
Comparison: Block implementation is written in parentheses when setting variables, rather than pointing to functions.
The callback function of C is just a pointer and must point to the function.
Supplement: Compared with the callback function of C, Block is more useful.
Image supplement:
C callback function:
1) CallBack. C
2) CallBack. h
3). main. c
OC Block
1) MyButton. h
2) MyButton. m
3) RootViewControl. m
4) Run