I would like to instantiate an Object with a given Type. Here's the call to the function:
buildObjetFromType("data", typeClass: User.self)
Here's the function code:
public static func buildObjectFromType<T>(data: String, typeClass: T.Type) -> T{
print(typeClass) // will print "User"
var object:T?
object as! T.Type // cast the object into an User -> NOT WORKING !
// fill the object with the data, knowing
// return the object once it has been filled with the data.
}
here's my User class:
public class User {
private var id:String?
private var login:String?
private var pwd:String?
private var nom:String?
private var prenom:String?
public required init(){
id = ""
login = ""
pwd = ""
nom = ""
prenom = ""
}
public convenience init(_login: String, _pwd: String){
self.init()
login = _login
pwd = _pwd
}
public convenience init(_id: String, _login: String, _pwd: String, _nom: String, _prenom: String){
self.init(_login: _login, _pwd: _pwd)
id = _id
nom = _nom
prenom = _prenom
}
}