Search code examples
cocoacocoa-touchswift

How to schedule a piece of code to execute in the next runloop in Swift?


There is no [NSRunLoop -performSelector:target:argument:order:modes:] in Swift currently. So does the [NSObject -performSelector:withObject:afterDelay:]. How can I schedule a piece of code to execute in the next runloop in Swift?


Solution

  • I figured out how to do so. Just enqueue a block based operation on the main queue with OperationQueue(NSOperationQueue in Cocoa).

    OperationQueue.main.addOperation { () -> Void in
        // Do your things
    }