Struct
I tried to recreate the codes in demo given by Chris Edihoff in dotSwift 2016 Demo. Here is the code.
struct List {
let name:String
let id:Int
}
extension List {
init?(json: [String:AnyObject]) {
guard
let name = json["name"] as? String,
let id = json["id"] as? Int else
{
return nil
}
self.name = name
self.id = id
}
}
let listData = json["data"] as? [[String:AnyObject]]
Everything seems fine till now. But this is where the problem comes. He did something like this:
let list:[List] = listData.flatMap(List.init)
The above line is supposed to be returning Array of List Objects. Chris Edihoff seems to be doing it without any problem, but when I do that Xcode warns
- flatMap produces 'U?', not the expected contextual result type '_?
Also what is List.init
here? I have never seen this way of initialisation of object. It should be List()
or if we are using customer initialization here, it should be List(json:someObject)
right?
Reference to Chris Edihof Talk: https://www.youtube.com/watch?v=ewk-XNzXzAA