我尝试学习如何使用GCD(THE DOC),并从apple文档中编写一些代码。但它无法在Xcode中编译。不确定我该怎么办呢。
- (void)viewDidLoad
{
[super viewDidLoad];
dispatch_source_type_t timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_main_queue());
if (timer) {
dispatch_source_set_timer(timer, dispatch_walltime(NULL, 0), 30ull * NSEC_PER_SEC, 1ull * NSEC_PER_SEC);
dispatch_source_set_event_handler(timer, ^{
NSLog(@"YES!");
});
dispatch_resume(timer);
}
// Do any additional setup after loading the view, typically from a nib.
}
错误:
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:22:36: Implicit conversion of Objective-C pointer type 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') to C pointer type 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') requires a bridged cast
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:22:28: Incompatible pointer types initializing 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') with an expression of type 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *')
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:25:35: Implicit conversion of C pointer type 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') to Objective-C pointer type 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') requires a bridged cast
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:25:35: Incompatible pointer types passing 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') to parameter of type 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *')
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:26:43: Implicit conversion of C pointer type 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') to Objective-C pointer type 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *') requires a bridged cast
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:26:43: Incompatible pointer types passing 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') to parameter of type 'dispatch_source_t' (aka 'NSObject<OS_dispatch_source> *')
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:29:25: Implicit conversion of C pointer type 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') to Objective-C pointer type 'dispatch_object_t' (aka 'NSObject<OS_dispatch_object> *') requires a bridged cast
/Users/sunfmin/Developments/GCDTests/GCDTests/ViewController.m:29:25: Incompatible pointer types passing 'dispatch_source_type_t' (aka 'const struct dispatch_source_type_s *') to parameter of type 'dispatch_object_t' (aka 'NSObject<OS_dispatch_object> *')
答案 0 :(得分:3)
调度源的类型为dispatch_source_t
,因此它应为
dispatch_source_t timer = ...
dispatch_source_type_t
是dispatch_source_create()
的第一个参数的类型,在您的情况下为DISPATCH_SOURCE_TYPE_TIMER
。