I have issue with following code:
func generic1(name : String){
}
func generic2(name : String){
generic1(name)
}
Typically there are many ways to define generic functions. But they are based on condition that T must be used as a parameter, or as a return type.
extension UIViewController {
class func doSomething() -> T {
return T()
}
class func doSomethingElse(value: T) {
// Note: value is a instance of T
}
class func doLastThing(value: T.Type) {
// Note: value is a MetaType of T
}
}
After that, we must provide T when calling.
let result = UIViewController.doSomething() as UIImageView // Define `T` by casting, as UIImageView
let result: UILabel = UIViewController.doSomething() // Define `T` with property type, as UILabel
UIViewController.doSomethingElse(value: UIButton()) // Define `T` with parameter type, as UIButton
UIViewController.doLastThing(value: UITextView.self) // Define `T` with parameter type, as UITextView
Ref: