I'm following this tutorial, especially I have problems to convert this function in Swift language:
- (id)init
{
CFRunLoopSourceContext context = {0, self, NULL, NULL, NULL, NULL, NULL,
&RunLoopSourceScheduleRoutine,
RunLoopSourceCancelRoutine,
RunLoopSourcePerformRoutine};
runLoopSource = CFRunLoopSourceCreate(NULL, 0, &context);
commands = [[NSMutableArray alloc] init];
return self;
}
of this, in the init function is context variable to give me problems.
In the code above, context is a variable of type : CFRunLoopSourceContext and the initialization of this object in the apple documentation is like this
so, I in my initialization, used the following code, concentrating on the schedule parameter :
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)
The function RunLoopSourceScheduleRoutine is like so :
func RunLoopSourceScheduleRoutine(info:UnsafeMutableRawPointer? ,rl:CFRunLoop? , mode:CFRunLoopMode?) {
let obj : RunLoopSource = Unmanaged<RunLoopSource>.fromOpaque(info!).takeUnretainedValue()
let theContext = RunLoopContext(withSource: obj, andLoop: rl!)
performSelector(onMainThread: #selector(myMethod), with: theContext, waitUntilDone: false)
}
but the compiler gives me the following error message : a c function pointer can only be formed from a reference to a 'func' or a literal closure
even if I make the following closure :
let runLoopSourceScheduleRoutine = { (info:UnsafeMutableRawPointer? ,rl:CFRunLoop? , mode:CFRunLoopMode?)-> Void in return
let obj : RunLoopSource = Unmanaged<RunLoopSource>.fromOpaque(info!).takeUnretainedValue()
let theContext = RunLoopContext(withSource: obj, andLoop: rl!)
performSelector(onMainThread: #selector(myMethod), with: theContext, waitUntilDone: false)
}
and i put it like this :
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)
gives me the same error. What is the problem ? any tips