[英]What does () mean in Swift?
I have the following function in Swift 3
我在
Swift 3
具有以下功能
func fetchOrders(_ completionHandler: (_ orders: [Order]) -> Void)
{
ordersStore.fetchOrders { (orders: () throws -> [Order]) -> Void in
do {
let orders = try orders()
completionHandler(orders)
} catch {
completionHandler([])
}
}
}
_ completionHandler
argument in fetchOrders
mean? _ completionHandler
在参数fetchOrders
是什么意思? (orders: () throws -> [Order])
mean? (orders: () throws -> [Order])
是什么意思? PS : I am new to iOS
and Swift
PS:我是
iOS
和Swift
新手
There's quite a lot in here, so we'll break it down one piece at a time: 这里有很多东西,所以我们一次将其分解:
func fetchOrders(_ completionHandler: (_ orders: [Order]) -> Void)
fetchOrders
. fetchOrders
的函数。 completionHandler
) and returns nothing. completionHandler
),什么也不返回。 _
indicates that there is no "external name" of the first parameter. _
表示第一个参数没有“外部名称”。 That is, you do not have to label it (in fact, you cannot). _
there, and I would not have done that.) _
犯了一个错误,我不会那样做。) completionHandler
is the "internal name," what the parameter is called inside the function. completionHandler
是“内部名称”,即在函数内部调用的参数。 completionHandler
is (_ orders: [Order]) -> Void
. completionHandler
是(_ orders: [Order]) -> Void
。 We'll break that down now. [Order]
(array of Order
) and returns Void
. [Order]
( Order
数组)并返回Void
的闭包。 Informally this means "returns nothing" but literally means it returns the empty tuple ()
. ()
。 _ orders:
syntax is in practice a comment. _ orders:
语法实际上是注释。 In principle the _
is an external name (but that's the only legal external name for a closure), and orders
is an internal name, but in reality, closures parameters do not have names in any meaningful way, so this is purely informational. _
是外部名称(但这是闭包的唯一合法外部名称), orders
是内部名称,但实际上,闭包参数没有任何有意义的名称,因此,这纯粹是信息性的。 orders
tells us nothing more than [Order]
, I would have omitted it, and made the type just ([Order]) -> Void
. orders
告诉我们[Order]
,因此我将省略它,而将类型设为([Order]) -> Void
。 Now we'll turn to the next line: 现在我们转到下一行:
ordersStore.fetchOrders { (orders: () throws -> [Order]) -> Void in
fetchOrders
method on ordersStore
. fetchOrders
的方法ordersStore
。 We can tell from this code that fetchOrders
takes a closure parameter. fetchOrders
一个闭包参数。 This is called "trailing closure" syntax in Swift, and is why I would not have used the _
for our closure. _
进行闭包的原因。 With trailing closure syntax, the external name of the parameter is not needed. { orders in
, but then the reader would probably have been surprised by this somewhat unusual code. { orders in
,但随后读者可能会对这个有点不寻常的代码感到惊讶。
orders
that takes nothing and returns [Order]
or throws an error. orders
是需要任何操作并返回[Order]
或抛出一个错误。 Basically this is a way to say that fetchOrders
might fail. fetchOrders
可能失败的一种方式。 throws
system, which does not have a natural way to express an asynchronous action that might fail. throws
系统中的尴尬,该系统没有自然的方式来表达可能失败的异步动作。 This is one way to fix it; Result
enum for this case because I think it scales better and avoids possible unintended side effects, but that's a debatable point (and the Swift community hasn't really decided how to deal with this common problem). Result
枚举,因为我认为它可以更好地扩展并避免可能的意外副作用,但这是有争议的一点(Swift社区尚未真正决定如何处理此问题。常见问题)。 This all leads us to: 这一切都导致我们:
do {
let orders = try orders()
completionHandler(orders)
} catch {
completionHandler([])
}
orders
closure is evaluated. orders
关闭的地方。 (This is very important; if orders
has side effects, this is when they occur, which may be on a different queue than was intended. That's one reason I don't favor this pattern.) If the closure succeeds, we return its result, otherwise we return []
in the catch
below. orders
有副作用,那就是它们发生的时间,可能与预期的发生在不同的队列上。这是我不赞成这种模式的原因之一。)如果关闭成功,则返回其结果,否则我们返回[]
在catch
下面。
throws
approach is slightly silly, because it's silently flattened into []
without even a log message. throws
方法有点愚蠢,因为它甚至在没有日志消息的情况下也被悄悄地压平为[]
。 If we don't care about the errors, then failure should have just returned []
to start with and not messed with throws
. []
而不是throws
。 But it's possible that other callers do check the errors. completionHandler
closure with our result, chaining this back to our original caller. completionHandler
闭包,并将其链接回我们的原始调用方。 This do/catch block could have been more simply written as: 这个do / catch块可以更简单地写为:
let completedOrders = try? orders() ?? []
completionHandler(completedOrders)
This makes it clearer that we're ignoring errors by turning it into an optional, and avoids code duplication of the call to completionHandler
. 这使得它更清晰,我们正在将其变成一个可选的忽略错误,避免调用的代码重复
completionHandler
。
(I just add the extra let
binding to make the code a little easier to read; it isn't needed.) (我只是添加了额外的
let
绑定,以使代码更易于阅读;不需要。)
completionHandler
参数意味着期望的参数(名为completionHandler
)必须是一个接受Order
对象列表且不返回任何值的函数。
completionHandler
is the a variable name. completionHandler
是一个变量名。 In this specific example, this variable is a callback . 在此特定示例中,此变量是callback 。 You know is a callback function because
(orders: [Order]) -> Void
is it's data type; 您知道这是一个回调函数,因为
(orders: [Order]) -> Void
是数据类型; in this particular case, said data type is a function that receives an array of Order
objects in a variable _orders
and doesn't have a return value (the Void
part). 在这种特殊情况下,所述数据类型是一个函数,该函数接收变量
_orders
的Order
对象数组,并且不具有返回值( Void
部分)。
TL;DR: TL; DR:
Order
as a parameter and acts as a callback. Order
数组作为参数并充当回调。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.