Learn OC block, and then write C callback function a bit awkward, compared to the difference, memory record.
C's callback function:
CallBack.h
1), defines 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, increment the number of runs and call the function pointer
void Dstcount (void) { static int numb = 0; numb++; (*MYCALLBACKPTR) (numb);}
Main.c
1), define a customdosomethingcount function, print the data
void Customdstcount (int data_i32) { printf ("%d\n", DATA_I32);}
2), main function, the CALLBACK.H function pointer to the customdstcount of Main, run CallBack.h dstcount function
Mycallbackptr = Customdstcount; while (1) { dstcount ();}
Advantages: 1, do not modify callback files, through main or other files can be modified, supplemented callback files.
Uses: 1, for example, a temperature sensor peripheral. C,.h are written, do not want others to modify it, set to read-only, by setting the callback function pointer, according to the need to set the filtering method, or the weight filter, or the average value filter.
Add: 1, callback function in the Ucos, as if called the hook (hooks) function, is not exactly the same, follow up to add it.
Block of OC:
MyButton.h (inherited from UIButton):
1), define a block of the number of parameters, type. (basically similar, c is *,oc is ^)
typedef void (^buttonblock) (int data_i32);
2), declare, define a block variable. (basically similar)
@property (Nonatomic,strong) Buttonblock mybuttonblockptr;
3), declare a dosomethingcount function. (basically similar)
-(void) dstcount;
MYBUTTON.M (inherited from UIButton):
1), in the Dosomethingcount function, increment the number of runs, 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, define a (100,100,100,100), red button instance, write the block of the button, print the number of times, button to add the event, the button added to the Self.view
MyButton *tempbutton = [[MyButton alloc]initwithframe:cgrectmake (+, +, +)]; 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, button click Call, Call MyButton in the Dstcount function
-(void) Mybuttonclick: (mybutton*) button{ [button Dstcount];}
Contrast: The implementation of block, written in parentheses when setting a variable, is not a pointer to a function.
C's callback function, just a pointer to the function.
Add: block is more useful than the callback function of C.
Photo Supplement:
C callback function:
1), CALLBACK.C
2), CallBack.h
3), MAIN.C
Block of OC
1), MyButton.h
2), MYBUTTON.M
3), ROOTVIEWCONTROL.M
4), run
Embedded &ios: callback function (C) vs. block (OC) callbacks