我正在阅读这篇文章http://chris.eidhof.nl/post/reducers/,当我发现一个令人费解的快速片段时,我意识到我无法理解。
鉴于这些枚举
enum Message {
case inputChanged(String?)
case ratesAvailable(data: Data?)
case reload
}
enum Command {
case load(URL, onComplete: (Data?) -> Message)
}
在以下函数中
mutating func send(_ message: Message) -> Command? {
switch message {
case .inputChanged(let input):
inputAmount = input.flatMap { Double($0) }
return nil
case .ratesAvailable(data: let data):
guard let data = data,
let json = try? JSONSerialization.jsonObject(with: data, options: []),
let dict = json as? [String:Any],
let dataDict = dict["rates"] as? [String:Double],
let rate = dataDict[Currency.usd.rawValue] else { return nil }
self.rate = rate
return nil
case .reload:
return .load(ratesURL(), onComplete: Message.ratesAvailable)
}
}
在此功能的最后一行。如何将枚举 Message.ratesAvailable 指定为命令枚举关联值中定义的clousure参数?
答案 0 :(得分:2)
因为Message.ratesAvailable
的类型为(Data?) -> Message
你可以see for yourself:
import Foundation
enum Message {
case inputChanged(String?)
case ratesAvailable(data: Data?)
case reload
}
print(type(of: Message.ratesAvailable)) // => (Optional<Data>) -> Message