Consider a Dictionarymotorcycle names and descriptions:
let data = ["Betty" : "Very fast", "Mike" : "Easy going"]
And a simple class Motorcyclewith two properties.
class Motorcycle {
let name: String
let description: String
init(name: String, description: String) {
self.name = name
self.description = description
}
}
Aa and a far-fetched function that takes two values Stringand returns a Motorcycle.
func genMotorcycle(name: String, desc: String) -> Motorcycle {
return Motorcycle(name: name, description: desc)
}
Now, assuming you want to convert Dictionaryto [Motorcycle], you could:
let motorcycles = map(data) { Motorcycle(name: $0, description: $1) }
Or, using the invented function genMotorcycle:
let motorcycles = map(data, genMotorcycle)
Since genMotorcyclefeels that he has the same type ( (String, String) -> Motorcycle) that initializer Motorcycle, I was wondering, is there a way to invoke the initializer Motorcycle, instead of, say, genMotorcycle.
In other words, is there a way to reliably express the following?
let motorcycles = map(data, Motorcycle)
let motorcycles = map(data, Motorcycle.init)