I have a question about fast and generics. What I'm trying to do is get an object with a common type. But first, I know the type at runtime. But quickly get to the place.
New edited block:
Maybe I can do this with the class name? I have a class name as a string. I got it through the mirror. Can I create a shared instance with this class name in a string?
let classname: String = "ClassA"
let firstA: a<classname> = a<classname>()
New edited block end:
I have two classes with common types:
This is the protocol that my type should implement:
protocol ToImplement {
func getTypeForKey(key: String) -> NSObject.Type
}
This is the class that I use for the first first type:
class MyClass: ToImplement {
func getTypeForKey(key: String) -> NSObject.Type {
if key == "key1" {
return UIView.self
}
else {
return UIButton.self
}
}
}
this is my first class with a generic type:
class a<T:ToImplement> {
func doSomethingWith(obj: T) -> T {
let type = obj.getTypeForKey("key1")
let newA: a<type> = a<type>()
return obj
}
}
and thatโs how I will use it:
let mycls: MyClass = MyClass()
let firstA: a<MyClass> = a<MyClass>()
firstA.doSomethingWith(mycls)
: a , ? ?
, . - :
let someA: a<instance.type> = a<instance.type>()
!