什么时候在 swift 中使用闭包?
When to use closures in swift?
我已经 ios 开发几个月了,渴望在我的编程模式中实现新事物。
现在我正在学习闭包并且对它的语法知之甚少,知道它可以代替委托用于回调。以及在一些 UIViewAnimation 中实现它并用于排序。
但我真的想知道它除了 that.i 之外的用途。e 我们应该在我们的基本编程中的什么地方使用闭包。就像我们在想要将信息从 child 发送到 parent 时使用委托一样。因此,可以在我们的日常 swift 编程中使用的关于其实际情况的任何解释或简短示例都是有帮助吗?
谁能告诉我这些闭包实际上是如何计算值的
reversed = sorted(names, { (s1: String, s2: String) -> Bool in return s1 > s2 } )
在这些示例中,名称和闭包作为方法的参数..但这实际上是如何计算的?
你能解释一下在这个动画代码中传递闭包时这些是如何工作的吗:
UIView.animateWithDuration(duration: NSTimeInterval,
animations: (() -> Void)?,
completion: ((Bool) -> Void)?)
我很想知道流量?
最常用的两种情况是 Swift 中的完成块和高阶函数。
完成块:例如,当您有一些耗时的任务时,您希望在该任务完成时得到通知。您可以为此使用闭包,而不是委托(或许多其他东西)
func longAction(completion: () -> ()) {
for index in veryLargeArray {
// do something with veryLargeArray, which is extremely time-consuming
}
completion() // notify the caller that the longAction is finished
}
//Or asynch version
func longAction(completion: () -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
for elem in veryLargeArray {
// do something with veryLargeArray, which is extremely time-consuming
}
dispatch_async(dispatch_get_main_queue(), {
completion() // notify the caller that the longAction is finished
})
}
}
longAction { print("work done") }
在上面的示例中,当您有一个耗时的任务时,您想知道 for 循环何时完成对非常大的数组的迭代。您将闭包 { println("work done") }
作为将在 for 循环完成其工作后执行的函数的输入参数,并打印 "work done"。发生的事情是你给 longAction 一个函数(闭包)并将其命名为 completion
,当你在 longAction.completion
中调用 completion
时,该函数将被执行。
高阶函数:您可以使用闭包作为高阶函数的输入参数,例如:
let array = [1, 2, 3]
let smallerThanTwo = array.filter { [=11=] < 2 }
这样就可以过滤掉小于2的数字
已更新 关于 sorted
(可能)的工作原理:
所以想法是,sorted 将遍历数组,并将两个连续元素 (i, i + 1) 相互比较,并在需要时交换它们。 "if needed" 是什么意思?您提供了闭包 { (s1: String, s2: String) -> Bool in return s1 > s2 }
,如果 s1
是 lexiographically 大于 s2
,它将 return true
。如果闭包 returned true
,sorted
算法将交换这两个元素,并将其与接下来的两个元素(i + 1,i + 2,如果结束未到达数组)。所以基本上你必须为 sorted
提供一个闭包,它会告诉 "when" 交换元素。
闭包类似于:
{ (params) -> returnType in
statements
}
以下是使用 Apple doc
的一些原因
- Inferring parameter and return value types from context
- Implicit returns from single-expression closures
- Shorthand argument names
- Trailing closure syntax
闭包通常没有名字,与其他函数不同。这意味着当您想将代码块传递给某个函数而不将该代码包装到命名方法中时,它们可以在任何情况下使用。排序是最流行的例子。
闭包可以使用其边界之外的变量。所谓"Capturing Values"
我已经 ios 开发几个月了,渴望在我的编程模式中实现新事物。
现在我正在学习闭包并且对它的语法知之甚少,知道它可以代替委托用于回调。以及在一些 UIViewAnimation 中实现它并用于排序。
但我真的想知道它除了 that.i 之外的用途。e 我们应该在我们的基本编程中的什么地方使用闭包。就像我们在想要将信息从 child 发送到 parent 时使用委托一样。因此,可以在我们的日常 swift 编程中使用的关于其实际情况的任何解释或简短示例都是有帮助吗?
谁能告诉我这些闭包实际上是如何计算值的
reversed = sorted(names, { (s1: String, s2: String) -> Bool in return s1 > s2 } )
在这些示例中,名称和闭包作为方法的参数..但这实际上是如何计算的?
你能解释一下在这个动画代码中传递闭包时这些是如何工作的吗:
UIView.animateWithDuration(duration: NSTimeInterval,
animations: (() -> Void)?,
completion: ((Bool) -> Void)?)
我很想知道流量?
最常用的两种情况是 Swift 中的完成块和高阶函数。
完成块:例如,当您有一些耗时的任务时,您希望在该任务完成时得到通知。您可以为此使用闭包,而不是委托(或许多其他东西)
func longAction(completion: () -> ()) {
for index in veryLargeArray {
// do something with veryLargeArray, which is extremely time-consuming
}
completion() // notify the caller that the longAction is finished
}
//Or asynch version
func longAction(completion: () -> ()) {
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0)) {
for elem in veryLargeArray {
// do something with veryLargeArray, which is extremely time-consuming
}
dispatch_async(dispatch_get_main_queue(), {
completion() // notify the caller that the longAction is finished
})
}
}
longAction { print("work done") }
在上面的示例中,当您有一个耗时的任务时,您想知道 for 循环何时完成对非常大的数组的迭代。您将闭包 { println("work done") }
作为将在 for 循环完成其工作后执行的函数的输入参数,并打印 "work done"。发生的事情是你给 longAction 一个函数(闭包)并将其命名为 completion
,当你在 longAction.completion
中调用 completion
时,该函数将被执行。
高阶函数:您可以使用闭包作为高阶函数的输入参数,例如:
let array = [1, 2, 3]
let smallerThanTwo = array.filter { [=11=] < 2 }
这样就可以过滤掉小于2的数字
已更新 关于 sorted
(可能)的工作原理:
所以想法是,sorted 将遍历数组,并将两个连续元素 (i, i + 1) 相互比较,并在需要时交换它们。 "if needed" 是什么意思?您提供了闭包 { (s1: String, s2: String) -> Bool in return s1 > s2 }
,如果 s1
是 lexiographically 大于 s2
,它将 return true
。如果闭包 returned true
,sorted
算法将交换这两个元素,并将其与接下来的两个元素(i + 1,i + 2,如果结束未到达数组)。所以基本上你必须为 sorted
提供一个闭包,它会告诉 "when" 交换元素。
闭包类似于:
{ (params) -> returnType in
statements
}
以下是使用 Apple doc
的一些原因
- Inferring parameter and return value types from context
- Implicit returns from single-expression closures
- Shorthand argument names
- Trailing closure syntax
闭包通常没有名字,与其他函数不同。这意味着当您想将代码块传递给某个函数而不将该代码包装到命名方法中时,它们可以在任何情况下使用。排序是最流行的例子。
闭包可以使用其边界之外的变量。所谓"Capturing Values"