Swift`in`关键字含义? [英] Swift `in` keyword meaning?
问题描述
我正在尝试从parse.com实现一些代码,并且在void之后我注意到中的关键字。
I am trying to implement some code from parse.com and I notice a keyword in
after the void.
我很难过这是什么?第二行你看到 Void in
I am stumped what is this ? The second line you see the Void in
PFUser.logInWithUsernameInBackground("myname", password:"mypass") {
(user: PFUser?, error: NSError?) -> Void in
if user != nil {
// Do stuff after successful login.
} else {
// The login failed. Check error to see why.
}
}
文档没有记录此内容。我知道中的关键字在
中用于
循环。
The docs don't document this. I know the in
keyword is used in for
loops.
有人确认吗?
推荐答案
在命名函数中,我们在中声明参数和返回类型func
申报行。
In a named function, we declare the parameters and return type in the func
declaration line.
func say(s:String)->() {
// body
}
在匿名函数中,没有 func
申报行 - 这是匿名的!所以我们在身体开头的行中使用代替。
In an anonymous function, there is no func
declaration line - it's anonymous! So we do it with an in
line at the start of the body instead.
{
(s:String)->() in
// body
}
(这是匿名函数的完整形式。但是Swift有一系列规则允许返回类型,参数类型,甚至参数名称在某些情况下,行中的行将被省略。)
(That is the full form of an anonymous function. But then Swift has a series of rules allowing the return type, the parameter types, and even the parameter names and the whole in
line to be omitted under certain circumstances.)
这篇关于Swift`in`关键字含义?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!