office API sample
Boolean CFReadStreamSetClient(CFReadStreamRef stream, CFOptionFlags streamEvents, CFReadStreamClientCallBack clientCB, CFStreamClientContext *clientContext);
callback function define
typedef void (*CFReadStreamClientCallBack)(CFReadStreamRef stream, CFStreamEventType type, void *clientCallBackInfo);
how to use?
1 define use typedef style
void AFReadStreamCallBack
(
CFReadStreamRef aStream,
CFStreamEventType eventType,
void* inClientInfo
);
void AFReadStreamCallBack
(
CFReadStreamRef aStream,
CFStreamEventType eventType,
void* inClientInfo
)
{
//do something
}
2 setup callback
CFStreamClientContext context = {0, (__bridge void *)(self), NULL, NULL, NULL};
CFReadStreamSetClient(
stream,
kCFStreamEventNone|kCFStreamEventHasBytesAvailable | kCFStreamEventErrorOccurred | kCFStreamEventEndEncountered|kCFStreamEventOpenCompleted,
AFReadStreamCallBack,
&context);
CFReadStreamScheduleWithRunLoop(stream, CFRunLoopGetCurrent(), kCFRunLoopCommonModes);
design callback sample code
#import "AppDelegate.h"
typedef void (*PF)(int x);
void func(int x)
{
NSLog(@"func x*x=%d",x*x);
}
void caller(PF pf,int a,int b)
{
int x = a+b;
pf(x);
}
@implementation AppDelegate
-(void)caller:(PF)pf a:(int)a b:(int) b
{
int x = a+b;
pf(x);
}
- (void)dealloc
{
[super dealloc];
}
- (void)applicationDidFinishLaunching:(NSNotification *)aNotification
{
PF p1 = func;
caller(p1,9,10);
[self caller:p1 a:199 b:8];
// Insert code here to initialize your application
}
caller 实现私有,指定义callback样式,不实现,具体实现是使用时候根据业务逻辑实现
C 语言的callback 和ObjC里面的protocol 是不是十分类似?














Recent Comments