-1
我有初始化结构中的问题,在夫特3.flatMap,结构和JSON在夫特3
从接收的URLRequest JSON数据的协议JSONDecodable:
protocol JSONDecodable {
init?(JSON: [String : AnyObject])
}
的结构是如下它实现扩展,符合JSONDecodable:
struct Actor {
let ActorID: String
let ActorName: String
}
extension Actor: JSONDecodable {
init?(JSON: [String : AnyObject]) {
guard let ActorID = JSON["ActorID"] as? String, let ActorName = JSON["ActorName"] as? String else {
return nil
}
self.ActorID = ActorID
self.ActorName = ActorName
}
}
然后,我有以下的代码,我试图映射字典的数组结构演员。我认为我将Swift 2和Swift 3语法混合在一起。
guard let actorsJSON = json?["response"] as? [[String : AnyObject]] else {
return
}
return actorsJSON.flatMap { actorDict in
return Actor(JSON: actorDict)
}
不过,我得到以下错误:'flatMap' produces '[SegmentOfResult.Iterator.Element]', not the expected contextual result type 'Void' (aka '()')
任何帮助将不胜感激!
在该方法中,你返回'flatMap'结果,它好像你忘词键入注释在函数签名的返回类型。现在,你还没有向我们展示这个函数,但要确保你的函数看起来不像'func myFunction(一些参数){...',而是'func myFunction(一些参数) - > [Actor] {。 ..'。 – dfri