0

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?

jsears
  • 4,511
  • 2
  • 31
  • 36
A O
  • 5,516
  • 3
  • 33
  • 68

1 Answers1

1

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.

Kazuki Sakamoto
  • 13,929
  • 2
  • 34
  • 96
  • But if you look here: http://stackoverflow.com/questions/959007/files-imported-when-importing-foundation-foundation-h-for-cocoa Cocoa.h doesn't contain dispatch.h-- yet I can still make dispatch calls without importing dispatch.h? – A O Sep 03 '15 at 02:36
  • Cocoa.h imports Foundation.h, right? Foundation.h imports NSOperation.h that I mentioned in the answer. So you already imports dispatch.h. – Kazuki Sakamoto Sep 03 '15 at 06:50
  • Right. So NSOperation.h imports dispatch.h? But my confusion is that it seems NSOperationQueues and Dispatch Queues are two entirely separate things – A O Sep 03 '15 at 14:50
  • See this thread too: stackoverflow.com/questions/9019327/… when is libSystem linked? If I look at the build phases, it doesn't include libSystem with libraries to be linked, so I can only assume Xcode automatically links some system libraries. Is there any way I can see a list of those? – A O Sep 03 '15 at 14:58
  • NSOperation.h uses `dispatch_queue_t` type. That's why NSOperation.h imports dispatch.h. That's it. No other reasons. – Kazuki Sakamoto Sep 03 '15 at 15:00
  • Nvm figured it out. it looks like dispatch.h is available through the Base SDK-- which has it's headers available through /usr/include. Then it's probably one of the first things linked after compiling – A O Sep 03 '15 at 15:08
  • I was afraid you would just leave after I did that-- because it still didn't make total sense to me. Accepting now ;) – A O Sep 03 '15 at 15:30