Is there a way to call a block with a primitive parameter after a delay, like using performSelector:withObject:afterDelay:
but with an argument like int
/double
/float
?
相关问题
- CALayer - backgroundColor flipped?
- Core Data lightweight migration crashes after App
- How can I implement password recovery in an iPhone
- State preservation and restoration strategies with
- “Zero out” sensitive String data in Swift
相关文章
- 现在使用swift开发ios应用好还是swift?
- UITableView dragging distance with UIRefreshContro
- TCC __TCCAccessRequest_block_invoke
- Where does a host app handle NSExtensionContext#co
- xcode 4 garbage collection removed?
- Unable to process app at this time due to a genera
- Swift - hide pickerView after value selected
- How can I add media attachments to my push notific
I think you're looking for
dispatch_after()
. It requires your block to accept no parameters, but you can just let the block capture those variables from your local scope instead.More: https://developer.apple.com/documentation/dispatch/1452876-dispatch_after
You can use
dispatch_after
to call a block later. In Xcode, start typingdispatch_after
and hitEnter
to autocomplete to the following:Here's an example with two floats as "arguments." You don't have to rely on any type of macro, and the intent of the code is quite clear:
Swift 3, Swift 4
Swift 2
Objective C
How about using Xcode built-in code snippet library?
Update for Swift:
Many up votes inspired me to update this answer.
The build-in Xcode code snippet library has
dispatch_after
for onlyobjective-c
language. People can also create their own Custom Code Snippet forSwift
.Write this in Xcode.
Drag this code and drop it in the code snippet library area.
Bottom of the code snippet list, there will be a new entity named
My Code Snippet
. Edit this for a title. For suggestion as you type in the Xcode fill in theCompletion Shortcut
.For more info see CreatingaCustomCodeSnippet.
Update Swift 3
Drag this code and drop it in the code snippet library area.
Here's the Swift 3 way to queue work after a delay.
Here is how you can trigger a block after a delay in Swift:
Its included as a standard function in my repo.
There's a nice one in the BlocksKit framework.
BlocksKit
(and the class)
BBlocksKit.m