Reputation: 436
Is there a way to dynamically pass a type parameter to a generic swift class? The below is just for illustration purposes from a playground which gives the error: use of undeclared type 'button'
import Cocoa
class PrintNameOfClass<T: NSObject> {
func printType() {
print(type(of: T.self))
}
}
let button = NSButton()
let test = PrintNameOfClass<button>()
test.printType()
Upvotes: 0
Views: 194
Reputation: 285059
In this case it's more reasonable to make the function generic rather than the class to be able to pass the instance button
class PrintNameOfClass {
func printNameOfClass<T : NSObject>(of : T) {
print(type(of: T.self))
}
}
let button = NSButton()
let test = PrintNameOfClass()
test.printNameOfClass(of: button)
Upvotes: 1