将参数传递给dispatch_async [英] Passing an argument to dispatch_async

查看:217
本文介绍了将参数传递给dispatch_async的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Swift的新手,看看dispatch_async函数是如何工作的。 API文档显示dispatch_async有两个参数。但是,我能够传递一个参数,这没关系。

I'm new to Swift and looking at how the dispatch_async function works. The API doc shows dispatch_async having two parameters. However, I'm able to pass in one argument and it's okay.

dispatch_async(dispatch_get_main_queue()) { 

}

为什么我不需要传递两个参数?

How come I don't need to pass in two arguments?

谢谢,

Thank you,

API Doc:

推荐答案

这是一个尾随闭包语法

func someFunctionThatTakesAClosure(closure: () -> ()) {
    // function body goes here
}

// here's how you call this function without using a trailing closure:

someFunctionThatTakesAClosure({
    // closure's body goes here
})

// here's how you call this function with a trailing closure instead:

someFunctionThatTakesAClosure() {
    // trailing closure's body goes here
}

https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/Swift_Programming_Language/Closures.html

这篇关于将参数传递给dispatch_async的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆