4
我连载一些JSON到对象中有failable JSON初始化器是这样的:地图只非空值
sections = {
let sectionJsons = json["sections"] as! [[String:AnyObject]]
return sectionJsons.map {
DynamicSection($0)
}
}()
DynamicSection的init:
init?(_ json:[String:AnyObject]) {
super.init()
//Boring stuff that can fail
我只想追加一条通过的dynamicSections初始化部分。我怎样才能做到这一点?
我可以使用filter
+ map
像
return sectionJsons.filter { DynamicSection($0) != nil }.map { DynamicSection($0)! }
但是,这导致INITING的DynamicSection的两倍,这是我想避免的。有没有更好的方法来做到这一点?
谢谢!不知道flatMap(Foo.init),这很整齐。 –