下面两个函数有什么区别?我将它们都输入到 Playground 上,但第二个函数没有输出任何内容。当您将 -> Bool
放在括号内部而不是外部时,这意味着什么?它是否不正确,因为我没有收到任何错误,但它也没有正确执行。我在使用完成处理程序的函数中看到了这一点,例如 CLGeocoder.reverseGeocodeLocation(_:completionHandler:)
但我不明白这意味着什么或如何正确使用它,如果有的话。
“正常”功能:
func aBasicFunction(string: String, anotherString: String) -> Bool {
// things happen
return true
}
我不明白的功能:
func aWeirdFunction(string: String, anotherString: String -> Bool) {
// things happen
return true // I think? Is this function actually supposed to return a Bool?
}
上面的第二个函数在我的 playground 中没有执行任何操作(在我实际的 playground 中它有一个 print 语句)但它也没有返回任何错误。
这是 CLGeocoder.reverseGeocodeLocation(_:completionHandler:)
函数:
CLGeocoder.reverseGeocodeLocation(currentLocation!, completionHandler: { (placemarks, error) -> Void in
if error == nil {
print(error)
}
// more stuff...
})
这可能是一个单独的问题,但我认为答案是相关的。 -> Void in
是什么意思?它在这里返回什么(或者在这种情况下不返回?
我还使用 Xcode 7 (Swift 2)。谢谢!
最佳答案
在 swift 中,函数是一等公民,可以作为参数传递给其他函数。
函数类型由语法 () -> ()
定义。例如,您可以定义一个函数,它接受整数并将它们的总和返回为 (Int, Int) -> Int
当你定义一个接受另一个函数作为参数的函数时,你需要定义允许传入的函数类型。
你的功能:
func aWeirdFunction(string: String, anotherString: String -> Bool) {
// things happen
return true
}
需要两个参数:String
类型的 string
和 (String) -> Bool
类型的 anotherString
,这实际上是一个函数,而不是一个字符串。由于您传递的是函数而不是字符串,因此您应该将参数名称重命名为 complete
或 callback
或其他函数名称。
func aWeirdFunction(string: String, complete: String -> Bool) {
// things happen
// return true <-- Nothing to return, actually
complete("some string")
}
然后,您可以像这样使用它:
aWeirdFunction("hello", complete: { response -> Bool in
print(response) // <-- prints "some string"
return true // must return a boolean
})
在 Swift 中,当最后一个参数是一个函数时,你可以简化语法,如下所示:
aWeirdFunction("hello") { response -> Bool in
print(response) // <-- prints "some string"
return true // must return a boolean
}
你也可以命名传递函数的参数:
func aWeirdFunction(string: String, complete: (anotherString: String) -> Bool) {
// things happen
// return true <-- Nothing to return, actually
complete(anotherString: "some string")
}
aWeirdFunction("hello") { anotherString -> Bool in
print(anotherString) // <-- prints "some string"
return true // must return a boolean
}
并允许具有多个参数的函数:
func aWeirdFunction(string: String, complete: (anotherString: String, total: Int) -> Bool) {
// things happen
// return true <-- Nothing to return, actually
complete(anotherString: "some string", total: 100)
}
aWeirdFunction("hello") { (anotherString, total) -> Bool in
print(anotherString, total) // <-- prints "some string", 100
return true // must return a boolean
}
最后,那些可以作为参数传递的函数也可以有返回值。在您的示例中,您指定传递的函数必须返回 Bool
,因为您的类型是 (String) -> Bool
。以下是您将如何使用它:
func aWeirdFunction(string: String, complete: (anotherString: String, total: Int) -> Bool) {
// things happen
// return true <-- Nothing to return, actually
let result = complete(anotherString: "some string", total: 100)
if result {
print("result is true!") // prints "result is true"
}
}
aWeirdFunction("hello") { (anotherString, total) -> Bool in
print(anotherString, total) // <-- prints "some string", 100
return true // must return a boolean
}
希望对您有所帮助!
奖金
如果您发现您的一个方法具有复杂的函数类型参数,或者您的许多方法需要相同的函数类型,您可以创建一个typealias
,这会增加很多清晰度:
typealias CompleteCallback = (anotherString: String, total: Int) -> Bool
func aWeirdFunction(string: String, complete: CompleteCallback) {
// things happen
// return true <-- Nothing to return, actually
let result = complete(anotherString: "some string", total: 100)
if result {
print("result is true!") // prints "result is true"
}
}
aWeirdFunction("hello") { (anotherString, total) -> Bool in
print(anotherString, total) // <-- prints "some string", 100
return true // must return a boolean
}
关于swift - 在 Swift 中定义函数时,如果 return 声明在括号内会发生什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33162576/