【发布时间】:2016-08-17 11:57:12
【问题描述】:
下面函数“parseB”中用于 flatMap 的语法使用括号,而不是大括号。
struct Episode {
let id: String
let title: String
}
extension Episode {
init?(dictionary: [String: AnyObject]) {
guard let id = dictionary["id"] as? String, title = dictionary["title"] as? String else { return nil }
self.id = id
self.title = title
}
}
func parseA(dictionaries: [[String: AnyObject]]) -> [Episode] {
return dictionaries.flatMap { dic in Episode.init(dictionary: dic)}
}
func parseB(dictionaries: [[String: AnyObject]]) -> [Episode] {
return dictionaries.flatMap (Episode.init)
}
有一个平面地图实现看起来像这样
public func flatMap<U>(@noescape f: (Wrapped) throws -> U!) rethrows -> U!
读到这里我还是不明白,在上面的例子中,Episode 的 init 是怎么没有括号的。
有时 Swift 非常擅长推断事物,以至于很难理解正在发生的事情。有人可以帮我理解为什么 Episodes 的 init 方法不使用括号吗?
...flatMap (Episode.init)
参考1:https://talk.objc.io/episodes/S01E01-networking 参考2:https://github.com/objcio/S01E01-networking
【问题讨论】: