Reputation: 3351
Is it possible to cast an object to a type which is stored in a member variable?
I've tried this:
let targetClass = type(of: MyTargetClass)
...
if anyObject is targetClass { // ERROR: use of undeclared type 'targetClass'
let test = anyObject as! targetClass // ERROR: use of undeclared type 'targetClass'
}
But it doesn't work because it says "use of undeclared type 'targetClass'"
Upvotes: 7
Views: 1128
Reputation: 42598
No. The type cast is a compile time thing. It lets the compiler know what methods are available and how to do the linking.
You can use Mirror
to gather information about your object at runtime, but it looks like invoking methods dynamically can still only be done on classes derived from NSObject
.
Upvotes: 5