简体   繁体   中英

Implicit conversion of Objective-C pointer type 'dispatch_source_t'

I try to learn how to use GCD ( THE DOC ), and write some of these code from apple document. But it can't compile in Xcode. Not sure how should I fix it.

- (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.
}

The errors:

/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> *')

The type of a dispatch source is dispatch_source_t , therefore it should be

dispatch_source_t timer = ...

dispatch_source_type_t is the type of the first argument to dispatch_source_create() , in your case DISPATCH_SOURCE_TYPE_TIMER .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM