I need to test whether the object is of type NSString or UIImageView. How can I accomplish this? Is there some type of "isoftype" method?
6 Answers
If your object is myObject, and you want to test to see if it is an NSString, the code would be:
[myObject isKindOfClass:[NSString class]] Likewise, if you wanted to test myObject for a UIImageView:
[myObject isKindOfClass:[UIImageView class]] 4 Comments
You would probably use
- (BOOL)isKindOfClass:(Class)aClass This is a method of NSObject.
For more info check the NSObject documentation.
This is how you use this.
BOOL test = [self isKindOfClass:[SomeClass class]]; You might also try doing somthing like this
for(id element in myArray) { NSLog(@"======================================="); NSLog(@"Is of type: %@", [element className]); NSLog(@"Is of type NSString?: %@", ([[element className] isMemberOfClass:[NSString class]])? @"Yes" : @"No"); NSLog(@"Is a kind of NSString: %@", ([[element classForCoder] isSubclassOfClass:[NSString class]])? @"Yes" : @"No"); } 3 Comments
className, it may or may not work on OS X, but it's not in iOS and it's not meant to be used for this. Also, those NSLogs don't do what I think you meant them to--they are checking the class name (always a string) and the coder class, strangely, instead of the class of the actual element.NSLog(@"Is of type: %@", [element class]); will work in iOS 7When you want to differ between a superClass and the inheritedClass you can use:
if([myTestClass class] == [myInheritedClass class]){ NSLog(@"I'm the inheritedClass); } if([myTestClass class] == [mySuperClass class]){ NSLog(@"I'm the superClass); } Using - (BOOL)isKindOfClass:(Class)aClass in this case would result in TRUE both times because the inheritedClass is also a kind of the superClass.
1 Comment
isMemberOfClass: will return NO when dealing with subclasses.Running a simple test, I thought I'd document what works and what doesn't. Often I see people checking to see if the object's class is a member of the other class or is equal to the other class.
For the line below, we have some poorly formed data that can be an NSArray, an NSDictionary or (null).
NSArray *hits = [[[myXML objectForKey: @"Answer"] objectForKey: @"hits"] objectForKey: @"Hit"]; These are the tests that were performed:
NSLog(@"%@", [hits class]); if ([hits isMemberOfClass:[NSMutableArray class]]) { NSLog(@"%@", [hits class]); } if ([hits isMemberOfClass:[NSMutableDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isMemberOfClass:[NSArray class]]) { NSLog(@"%@", [hits class]); } if ([hits isMemberOfClass:[NSDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSMutableDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSDictionary class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSArray class]]) { NSLog(@"%@", [hits class]); } if ([hits isKindOfClass:[NSMutableArray class]]) { NSLog(@"%@", [hits class]); } isKindOfClass worked rather well while isMemberOfClass didn't.
2 Comments
Simple, [yourobject class] it will return the class name of yourobject.
1 Comment
Class object. However, the description of this object will be the class name as a string, so you can therefore still log it to the console.
[object isKindOfClass:[ClassName class]]