如果您不想在NO
应在 5 秒延迟后返回的情况下阻塞主线程,则异步构造该 API。
typedef void(^CCFVerificationCallbackBlock)(BOOL verified);
@interface CCFVerifier : NSObject
- (void)verifyLabel:(NSString *)label withCallbackBlock:(CCFVerificationCallbackBlock)block;
@end
static const int64_t ReturnDelay = 5.0 * NSEC_PER_SEC;
@implementation CCFVerifier
- (void)verifyLabel:(NSString *)label withCallbackBlock:(CCFVerificationCallbackBlock)block {
NSParameterAssert(block);
if( [label isEqualToString:@"moo"] )
block(YES);
else {
dispatch_time_t popTime = dispatch_time(DISPATCH_TIME_NOW, ReturnDelay);
dispatch_after(popTime, dispatch_get_main_queue(), ^(void){
block(NO);
});
}
}
@end
要使用:
_verifier = [[CCFVerifier alloc] init];
[_verifier verifyLabel:@"foo" withCallbackBlock:^(BOOL verified) {
NSLog(@"verification result: %d",verified);
}];