title: Function pointer in swift
body: <p>I'm following this <a href="https://developer.apple.com/library/content/documentation/Cocoa/Conceptual/Multithreading/RunLoopManagement/RunLoopManagement.html#//apple_ref/doc/uid/10000057i-CH16-SW3" rel="nofollow noreferrer">tutorial</a>, especially I have problems to convert this function in Swift language:</p>
  <pre><code>- (id)init
  {
      CFRunLoopSourceContext    context = {0, self, NULL, NULL, NULL, NULL, NULL,
                                      &amp;RunLoopSourceScheduleRoutine,
                                      RunLoopSourceCancelRoutine,
                                      RunLoopSourcePerformRoutine};
  
      runLoopSource = CFRunLoopSourceCreate(NULL, 0, &amp;context);
      commands = [[NSMutableArray alloc] init];
  
      return self;
  }
  </code></pre>
  <p>of this, in the init function is <code>context</code> variable to give me problems.</p>
  <p>In the code above, context is a variable of type : <code>CFRunLoopSourceContext</code> and the initialization of this object in the apple documentation is <a href="https://developer.apple.com/reference/corefoundation/cfrunloopsourcecontext" rel="nofollow noreferrer">like this</a></p>
  <p>so, I in my initialization, used the following code, concentrating on the <code>schedule</code> parameter :</p>
  <pre><code>var context = CFRunLoopSourceContext(version: 0, info: bridge(obj: self) ,
                                           retain: nil,
                                           release: nil,
                                           copyDescription: nil,
                                           equal: nil,
                                           hash: nil,
                                           schedule: RunLoopSourceScheduleRoutine,
                                           cancel: nil,
                                           perform: nil)
  </code></pre>
  <p>The function <code>RunLoopSourceScheduleRoutine</code> is like so :</p>
  <pre><code>    func RunLoopSourceScheduleRoutine(info:UnsafeMutableRawPointer? ,rl:CFRunLoop? , mode:CFRunLoopMode?)  {
      
      let obj :  RunLoopSource = Unmanaged&lt;RunLoopSource&gt;.fromOpaque(info!).takeUnretainedValue()
      let theContext = RunLoopContext(withSource: obj, andLoop: rl!)
      performSelector(onMainThread: #selector(myMethod), with: theContext, waitUntilDone: false)
      
  }
  </code></pre>
  <p>but the compiler gives me the following error message : <code>a c function pointer can only be formed from a reference to a 'func' or a literal closure</code></p>
  <p>even if I make the following closure :</p>
  <pre><code>let runLoopSourceScheduleRoutine = { (info:UnsafeMutableRawPointer? ,rl:CFRunLoop? , mode:CFRunLoopMode?)-&gt; Void in return
          
          let obj :  RunLoopSource = Unmanaged&lt;RunLoopSource&gt;.fromOpaque(info!).takeUnretainedValue()
          let theContext = RunLoopContext(withSource: obj, andLoop: rl!)
          performSelector(onMainThread: #selector(myMethod), with: theContext, waitUntilDone: false)
          
      }
  </code></pre>
  <p>and i put it like this :</p>
  <pre><code>var context = CFRunLoopSourceContext(version: 0, info: bridge(obj: self) ,
                                           retain: nil,
                                           release: nil,
                                           copyDescription: nil,
                                           equal: nil,
                                           hash: nil,
                                           schedule: runLoopSourceScheduleRoutine,
                                           cancel: nil,
                                           perform: nil)
  </code></pre>
  <p>gives me the same error.
  What is the problem ?
  any tips</p>