Clicky

iOS Dev Nugget 124 performSelector:withObject:afterDelay: in Swift

.

Need to run a code review on your codebase? Hire me

-performSelector:withObject:afterDelay: isn't available in Swift. Generally, it's a bad idea to use -performSelector: and it's variants, but there are exceptions, especially since not all code ends up in production. I find it particularly helpful during development to use it to auto-navigate to the screen I am currently working on. Courtesy of Stack Overflow, in Swift, we can use dispatch_time() to build something similar.

func delay(delay:Double, closure:()->()) {
    dispatch_after(
        dispatch_time(DISPATCH_TIME_NOW, Int64(delay * Double(NSEC_PER_SEC))),
        dispatch_get_main_queue(),
        closure)
}

and call it like this:

delay(0.7) {
    //do something here
}


Your feedback is valuable: Do you want more nuggets like this?   Yes   or   No

.

.

Like this and want such iOS dev nuggets to be emailed to you, weekly?

Sign Me Up! or follow @iosdevnuggets on Twitter

.

View archives of past issues