Objective-C has two methods to test if an object is an instance of a specific class or a subclass:
- (BOOL)isMemberOfClass:(Class)aClass;
Returns a Boolean value that indicates whether the receiver is an instance of a given class.
- (BOOL)isKindOfClass:(Class)aClass;
Returns a Boolean value that indicates whether the receiver is an instance of given class or an instance of any class that inherits from that class.
In Swift I can test for the latter by using the is
operator:
if myVariable is UIView {
println( "I'm a UIView!")
}
if myVariable is MyClass {
println( "I'm a MyClass" )
}
How can I test if an instance is a specific class or type in Swift (even when dealing with no NSObject subclasses)?
Note: I'm aware of func object_getClassName(obj: AnyObject!) -> UnsafePointer<Int8>
.