I've gone through the iBook from Apple, and couldn't find any definition of it :
Can someone explain the structure of dispatch_after?
dispatch_after(<#when: dispatch_time_t#>, <#queue: dispatch_queue_t?#>, <#block: dispatch_block_t?#>)
We started with Q&A. Technical documentation is next, and we need your help.
Whether you're a beginner or an experienced developer, you can contribute.
|
I've gone through the iBook from Apple, and couldn't find any definition of it : Can someone explain the structure of
|
||||
|
|
|
A clearer idea of the structure:
Example usage:
EDIT: I recommend using @matt's really nice EDIT 2: In Swift 3, there will be new wrappers for GCD. See here: https://github.com/apple/swift-evolution/blob/master/proposals/0088-libdispatch-for-swift3.md The original example would be written as follows in Swift 3:
Note that you can write the
This means that if you don't use the |
|||||||||||||||||||||
|
|
I use
And now you can talk like this:
Wow, a language where you can improve the language. What could be better? Update for Swift 3, Xcode 8 Seed 6Seems almost not worth bothering with, now that they've improved the calling syntax:
|
|||||||||||||||||||||
|
|
matt's syntax is very nice and if you need to invalidate the block, you may want to use this :
Use as follow
Link above seems to be down. Original Objc code from Github |
|||||||||||||||||||||
|
|
To expand on Cezary's answer, which will execute after 1 nanosecond, I had to do the following to execute after 4 and a half seconds.
Edit: I discovered that my original code was slightly wrong. Implicit typing causes a compile error if you don't cast NSEC_PER_SEC to a Double. If anyone can suggest a more optimal solution I'd be keen to hear it. |
||||
|
Another way is to extend Double like this:
Then you can use it like this:
I like matt's delay function but just out of preference I'd rather limit passing closures around. |
|||
|
|
|
Apple has a dispatch_after snippet for Objective-C:
Here is the same snippet ported to Swift:
|
|||
|
|
|
1) Add this method as a part of UIViewController Extension.
Call this method on VC:
2) performSelector("yourMethod Name", withObject: nil, afterDelay: 1) 3)
//Compact Form
} |
||||
|
|
|
Although not the original question by the OP, certain
|
|
In Swift 3.0 Dispatch queues
Dispatch after 5 seconds
|
|||
|
|
|
Another helper to delay your code that is 100% Swift in usage and optionally allows for choosing a different thread to run your delayed code from:
Now you simply delay your code on the Main thread like this:
If you want to delay your code to a different thread:
If you prefer a Framework that also has some more handy features then checkout HandySwift. You can add it to your project via Carthage then use it exactly like in the examples above, e.g.:
|
|||
|
|
|
use this code to perform some UI related task after 2.0 seconds.
|
|||||
|
Thank you for your interest in this question.
Because it has attracted low-quality or spam answers that had to be removed, posting an answer now requires 10 reputation on this site (the association bonus does not count).
Would you like to answer one of these unanswered questions instead?