views:

392

answers:

3
NSMutableArray *arr = [NSMutableArray arrayWithCapacity:3];

if ([arr isMemberOfClass:[NSMutableArray class]]) {
    NSLog(@"YEP!!");
} else {
    NSLog(@"NO!!");
}

I get always "NO!!" in console. I tried with Array, NSNumber, NSString. All of them don't seem to work. That's strange. As I get it from the docs, this method should return YES if I test agains the class of the object which is subject of the testing. I'm using the Foundation.h, not Core Foundation (if it makes any difference anyways).

+1  A: 

did you try isKindOfClass instead of isMemberOfClass?

ennuikiller
+5  A: 

You need to use isKindOfClass: or respondsToSelector: as NSArray and similar are class clusters - you actually get subclasses when creating them. See this answer for some interesting bits on this.

Georg Fritzsche
@gf, what is a "class cluster" ?
Gollum
See the apple documentation: http://developer.apple.com/Mac/library/documentation/Cocoa/Conceptual/CocoaFundamentals/CocoaObjects/CocoaObjects.html#//apple_ref/doc/uid/TP40002974-CH4-SW34
Georg Fritzsche
+2  A: 

When you create an array using [NSMutableArray arrayWithCapacity:], the object you get is actually a member of some concrete subclass of NSMutableArray. NSMutableArray only exists momentarily in the process of creating the concrete instance.

-isMemberOfClass: returns true if the receiver's isa pointer is equal to the given class. -isKindOfClass: returns true if the receiver is a member of the given class or any of its subclasses.

NSResponder