In Swift 3, I had a snipped of code that called into Objective-C runtime to check if a certain class is present.
guard let managerClass = NSClassFromString("ASIdentifierManager") as? NSObjectProtocol else {
return nil
}
This returns AnyClass
instance and it is castable to NSObjectProtocol
- so I could call methods like responds
and perform
to work with selectors.
In Swift 4, this still works at runtime, but will emit a warning:
Cast from 'AnyClass?' (aka 'Optional') to unrelated type 'NSObjectProtocol' always fails
Oddly it still works, but I am worried a certain Swift version will kill the code.
What is the proper way to get rid of this warning? To what should I cast the object, to be able to perform selectors on it dynamically?
NSObjectProtocol.Type
. – ZygoteNSObjectProtocol.Type
, but rather it is an instance ofNSObjectProtocol
. – Cho