Search code examples
objective-ccocoagrand-central-dispatchlibdispatch

Where is libdispatch AKA Grand Central Dispatch imported?


I am reading Apple's documentation on Concurrency, more specifically on NSOperationQueue vs a Dispatch Queue.

They say this:

"An operation queue is the Cocoa equivalent of a concurrent dispatch queue ..."

Which made me wonder, if NSOperationQueue is available through the Cocoa framework, how is GCD available?

When I call dispatch_after..., why is it that I don't need to #import <Cocoa/Cocoa.h>?

How does the compiler recognize GCD selectors?


Solution

  • You need to include dispatch/dispatch.h header file for using GCD APIs.

    dispatch manual:

    $ man dispatch
    dispatch(3)              BSD Library Functions Manual              dispatch(3)
    
    NAME
         dispatch -- the dispatch framework
    
    SYNOPSIS
         #include <dispatch/dispatch.h>
    

    Cocoa headers also import the header file (For example, NSOperation.h), so in most cases you don't need to import the header file explicitly.